sandy1028 Posted August 7, 2007 Share Posted August 7, 2007 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 More sharing options...
matto Posted August 7, 2007 Share Posted August 7, 2007 you are using the same variable for both name and address (name) - you may want to use separate variables for each column name=asc address=asc Link to comment https://forums.phpfreaks.com/topic/63697-asc-and-desc/#findComment-317424 Share on other sites More sharing options...
sandy1028 Posted August 7, 2007 Author Share Posted August 7, 2007 I have used name=asc address=asc. I want the sort order not to change unless clicked on the particular column Link to comment https://forums.phpfreaks.com/topic/63697-asc-and-desc/#findComment-317433 Share on other sites More sharing options...
AndyB Posted August 7, 2007 Share Posted August 7, 2007 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 More sharing options...
sandy1028 Posted August 7, 2007 Author Share Posted August 7, 2007 Hi, Thanks for the reply How to use the request in if statement Link to comment https://forums.phpfreaks.com/topic/63697-asc-and-desc/#findComment-317449 Share on other sites More sharing options...
sandy1028 Posted August 7, 2007 Author Share Posted August 7, 2007 Hi, I am using the columns sorting separate for each column of table. ie each column sorted is independent. How to use the querystring passed in if statement Link to comment https://forums.phpfreaks.com/topic/63697-asc-and-desc/#findComment-317452 Share on other sites More sharing options...
AndyB Posted August 7, 2007 Share Posted August 7, 2007 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 More sharing options...
sandy1028 Posted August 7, 2007 Author Share Posted August 7, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.