Jump to content

Asc and desc


sandy1028

Recommended Posts

Hi,

 

How to sort the column of table ascending and descending. The column should be sorted independently.

When only clicked on the column header the sort order should change.

How can it be done.

 


echo "<td><a href=\"tree.php?name=asc\">Name</a></td>";
}
else if($name == "asc"){
echo "<td><a href=\"tree.php?name=desc\">Name</a></td>";
if($name != "asc"){

//Some code of descending sorting

}
else if {
//Some code of sorting
}

echo "<td><a href=\"tree.php?name=asc\">Address</a></td>";
}
else if($name == "asc"){
echo "<td><a href=\"tree.php?name=desc\">Address</a></td>";


 

Problem is when I click on the Address header the sort of Name changes to descending.

The sorting should not change unless clciked on the Name header field

 

How can we do it

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/63697-asc-and-desc/
Share on other sites

The problem is that your links don't differentiate which parameter you want to use for sorting.  The example below passes ONLY the sort direction.

 

<a href=\"tree.php?name=asc\">Name</a>
...
<a href=\"tree.php?name=asc\">Address</a>[code]";

What you need to do is to pass a fieldname and a sort direction, e.g.

[code]<a href=\"tree.php?var=name&sort=asc\">Name</a>

[/code][/code]

Link to comment
https://forums.phpfreaks.com/topic/63697-asc-and-desc/#findComment-317445
Share on other sites

You don't need "ifs".

 

Suppose a link like tree.php?var=name&sort=asc is clicked.  Process it thus:

 

<?php
$var = $_GET['var'];
$sort = $_GET['sort'];

// connect to SQL, select database, etc.

$query = "SELECT * from tablename ORDER by ". $var. " ". $sort;
$result = mysql_query($query);
etc,etc

Link to comment
https://forums.phpfreaks.com/topic/63697-asc-and-desc/#findComment-317455
Share on other sites

Hi,

 

I am fetching data from the text files. Give me solution to this....

I am not using mysql

 

 

 

$lines=file("./data.txt");
foreach($lines as $line)
{
$text_line=explode(":",$line);
$text_array[]=array('names'=>$text_line[0], 'addresss'=>$text_line[1],'ages'=>$text_line[2], 'emails'=>$text_line[3],'mobiles'=>$text_line[4],'residences'=>$text_line[5], 'key'=>$key);

foreach ($text_array as $key => $row) {
$names[$key]  = $row['names'];
$addresss[$key] = $row['addresss'];
$ages[$key] = $row['ages'];
$emails[$key] = $row['emails'];
$mobiles[$key] = $row['mobiles'];
$residences[$key] = $row['residences'];
}
}
array_multisort($names, SORT_ASC,SORT_STRING, $text_array);

Link to comment
https://forums.phpfreaks.com/topic/63697-asc-and-desc/#findComment-317459
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.