TheStalker Posted December 4, 2008 Share Posted December 4, 2008 hi guys, ok i have a table and want the table headers to be links so that when they are clicked they change the order of the table to ASC then DESC and back again, and so on and so on. Can anyone help? i can get the table to switch from ASC to DESC but not back again ? Here is the code i have atm i know its not right but im trying to keep things as simple as i can. <?php $sql = mysql_query("SELECT* FROM table WHERE surname LIKE '$selection%' AND link ='1' ORDER BY surname ASC"); ?> <table border='1' width =''> <tr> <th><a href ="test.php?sort=1<?php //echo $selection;?>">name</a></th> <th>ref</th> <th>DOB</th> </tr> <?php $sort = $_GET['sort']; if ($sort = $sort) $sql= mysql_query("SELECT* FROM table WHERE surname LIKE '$selection%' AND link ='1' ORDER BY surname DESC"); else $sql = mysql_query("SELECT* FROM table WHERE surname LIKE '$selection%' AND link ='1' ORDER BY surname ASC"); ?> Any help would be good. I know i might be going about it all wrong..... Quote Link to comment https://forums.phpfreaks.com/topic/135498-solved-order-by-asc-then-desc-and-back-again/ Share on other sites More sharing options...
Mchl Posted December 4, 2008 Share Posted December 4, 2008 if ($sort = $sort) That is obviously always true. On the side note: you might try one of several datagrid widgets, that have this functionality built in. Quote Link to comment https://forums.phpfreaks.com/topic/135498-solved-order-by-asc-then-desc-and-back-again/#findComment-705896 Share on other sites More sharing options...
revraz Posted December 4, 2008 Share Posted December 4, 2008 Maybe if ($sort = "DESC") Depending on how you send the variable in your URL Quote Link to comment https://forums.phpfreaks.com/topic/135498-solved-order-by-asc-then-desc-and-back-again/#findComment-705902 Share on other sites More sharing options...
TheStalker Posted December 4, 2008 Author Share Posted December 4, 2008 if ($sort = $sort) That is obviously always true. On the side note: you might try one of several datagrid widgets, that have this functionality built in. sorry should of been ($sort == '1') need to work out how to change th><a href ="test.php?sort=1 to 0 every other time its clicked or something like that Quote Link to comment https://forums.phpfreaks.com/topic/135498-solved-order-by-asc-then-desc-and-back-again/#findComment-705905 Share on other sites More sharing options...
revraz Posted December 4, 2008 Share Posted December 4, 2008 Store the current sort in a session, will be simple from there. Quote Link to comment https://forums.phpfreaks.com/topic/135498-solved-order-by-asc-then-desc-and-back-again/#findComment-705919 Share on other sites More sharing options...
Mchl Posted December 4, 2008 Share Posted December 4, 2008 <?php session_start(); if(isset($_GET['togglesort'])) { $_SESSION['sortDir'] = 1 - $_SESSION['sortDir']; //toggle sort order } else { $_SESSION['sortDir'] = 0 //default sort order } if ( $_SESSION['sortDir'] = 0) { $sortDir = "ASC"; } else { $sortDir = "DESC"; } $query = "SELECT * FROM table WHERE surname LIKE '$selection%' AND link ='1' ORDER BY surname $sortDir"; //fetch data ?> //display your table <table border='1' width =''> <tr> <th><a href ="test.php?togglesort=1">name</a></th> <th>ref</th> <th>DOB</th> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/135498-solved-order-by-asc-then-desc-and-back-again/#findComment-705926 Share on other sites More sharing options...
.josh Posted December 4, 2008 Share Posted December 4, 2008 <?php session_start(); if ($_GET['dir']) { $_SESSION['dir'] = ($_SESSION['dir'] == 'ASC')? 'DESC' : 'ASC'; } Quote Link to comment https://forums.phpfreaks.com/topic/135498-solved-order-by-asc-then-desc-and-back-again/#findComment-705934 Share on other sites More sharing options...
TheStalker Posted December 5, 2008 Author Share Posted December 5, 2008 <?php session_start(); if ($_GET['dir']) { $_SESSION['dir'] = ($_SESSION['dir'] == 'ASC')? 'DESC' : 'ASC'; } Cheers for all the help guys ended up using this code if(isset($_GET["sort"])) $sort = ($_GET["sort"]=="DESC")?"ASC":"DESC"; else { $sort="ASC"; } Quote Link to comment https://forums.phpfreaks.com/topic/135498-solved-order-by-asc-then-desc-and-back-again/#findComment-706601 Share on other sites More sharing options...
.josh Posted December 5, 2008 Share Posted December 5, 2008 <?php session_start(); if ($_GET['dir']) { $_SESSION['dir'] = ($_SESSION['dir'] == 'ASC')? 'DESC' : 'ASC'; } Cheers for all the help guys ended up using this code if(isset($_GET["sort"])) $sort = ($_GET["sort"]=="DESC")?"ASC":"DESC"; else { $sort="ASC"; } Did you even bother to try that? If you set the default to ASC it will sort as ASC on first page load. If you make your link pass ASC it will switch sort as DESC if you click the link. Now click the link again and guess what, it sorts it as DESC again. And DESC again. And if you change your link to pass DESC, it will just keep sorting as ASC over and over and over. In order for 'your script' to 'remember' whether to turn the proverbial light switch on or off, it has to know what state it's currently in (on or off). But clicking on a link makes a new request to the server. As far as php and your script is concerned, it's a completely new request that has nothing to do with the previous request. There is no 'state' to speak of. In order for a single link 'switch' to work, you have to tell it what state the switch was in, before you clicked the link and restarted the whole script. In order to do that, you need to use some means of data persistence. You can do this with a cookie, but it's easier and less hassle to do this with a session variable, as shown to you by multiple people. Quote Link to comment https://forums.phpfreaks.com/topic/135498-solved-order-by-asc-then-desc-and-back-again/#findComment-706699 Share on other sites More sharing options...
TheStalker Posted December 9, 2008 Author Share Posted December 9, 2008 I did try it yeh, and i am really greatful for all the help. i only posted up what i come up with in case any one else finds this post. I see where your coming from and i am still working on the page so it could well change. Thanks again for all the good help Quote Link to comment https://forums.phpfreaks.com/topic/135498-solved-order-by-asc-then-desc-and-back-again/#findComment-710370 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.