sleepyw Posted June 3, 2009 Share Posted June 3, 2009 I have a PHP/MySQL search results page output to multiple columns in an HTML table. Each column header is a link to resort the results based on that column. However, I'm trying to figure out how to create the code to where the column will toggle between ascending and descending each time the link is clicked. My headers for each row of results look like this: <td><a href="index.php?sortby=fieldname">Field Name</a></td> How can I get this to toggle back and forth (by default, it sorts by ascending). Quote Link to comment https://forums.phpfreaks.com/topic/160825-sorting-results-with-an-ascendingdescending-toggle/ Share on other sites More sharing options...
lonewolf217 Posted June 3, 2009 Share Posted June 3, 2009 You can look into using jQuery and tableSorter plugin http://tablesorter.com/docs/ Quote Link to comment https://forums.phpfreaks.com/topic/160825-sorting-results-with-an-ascendingdescending-toggle/#findComment-848815 Share on other sites More sharing options...
sleepyw Posted June 3, 2009 Author Share Posted June 3, 2009 I found a site online that shows what I'm trying to do: http://www.plus2net.com/sql_tutorial/sql_order-change.php?by=asc Problem is my page table and queries are set up differently, so when I implement his code, it only seems to work once. It isn't continually grabbing the variable and changing it. Quote Link to comment https://forums.phpfreaks.com/topic/160825-sorting-results-with-an-ascendingdescending-toggle/#findComment-848838 Share on other sites More sharing options...
Maq Posted June 3, 2009 Share Posted June 3, 2009 I found a site online that shows what I'm trying to do: http://www.plus2net.com/sql_tutorial/sql_order-change.php?by=asc Problem is my page table and queries are set up differently, so when I implement his code, it only seems to work once. It isn't continually grabbing the variable and changing it. Can we see your relevant current code? Quote Link to comment https://forums.phpfreaks.com/topic/160825-sorting-results-with-an-ascendingdescending-toggle/#findComment-848839 Share on other sites More sharing options...
sleepyw Posted June 3, 2009 Author Share Posted June 3, 2009 I found a site online that shows what I'm trying to do: http://www.plus2net.com/sql_tutorial/sql_order-change.php?by=asc Problem is my page table and queries are set up differently, so when I implement his code, it only seems to work once. It isn't continually grabbing the variable and changing it. Can we see your relevant current code? I keep altering it to try and get it to work. It looks something like this, though (the %20 I threw in to see if it would help based on the way it parses in the browser url field): <?php if (($by == " ASC") OR ($by == "%20ASC")) { $by = " DESC";} else if (($by == " DESC") OR ($by == "%20DESC")) { $by = " ASC";} else if (($by == "")) { $by = " ASC";}?> <td><a href="index.php?sortby=Field_name<? echo $by; ?>&by=<? echo $by; ?>">Field name</a></td> Then in the query, the order by = $sortby. Quote Link to comment https://forums.phpfreaks.com/topic/160825-sorting-results-with-an-ascendingdescending-toggle/#findComment-848845 Share on other sites More sharing options...
sleepyw Posted June 3, 2009 Author Share Posted June 3, 2009 You can look into using jQuery and tableSorter plugin http://tablesorter.com/docs/ I can actually do this myself if I create 2 buttons/arrows to the right of each field, then hard code the ASC or DESC, but I have limited space and am trying to do this with a single clickable link, i.e., the column name. Thanks for looking into it, though. Quote Link to comment https://forums.phpfreaks.com/topic/160825-sorting-results-with-an-ascendingdescending-toggle/#findComment-848846 Share on other sites More sharing options...
sleepyw Posted June 3, 2009 Author Share Posted June 3, 2009 Got it! I had to place this at the top of the page: <?php $by=$_GET['by']; ?> Then this: <?php if(isset($by) and $by==" ASC"){ $by=" DESC"; }else{$by=" ASC";} ?> Then the table column is: <td><a href="index.php?sortby=Field_name<? echo $by; ?>&by=<? echo $by; ?>">Field name</a></td> And at the end of each query is: ORDER BY $sortby Quote Link to comment https://forums.phpfreaks.com/topic/160825-sorting-results-with-an-ascendingdescending-toggle/#findComment-848863 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.