Quote:
Originally Posted by SlayerACC
How can I create a sort button that sorts DESCENDING by default but when clicked on the page changes to ASCENDING and vise versa depending the current value?
|
Quote:
Originally Posted by weir-07
PHP Code:
if ($_GET['sortby']) { $sql_string="ASC"; } else { $sql_string="DESC"; }
|
So when the user clicks the button it switches to DESC right? So what happens when they click it again? - It will still set to desc again won't it because the $_GET['sortby'] has been sent again
Slayer, your code is also going to cause parse errors..:
Code:
<div class="sortbtns\"><a href='{$_SERVER['PHP_SELF']}?sortby=year'>Year</a></div>
Should be:
Code:
<div class=\"sortbtns\"><a href=\"{$_SERVER['PHP_SELF']}?sortby=year\">Year</a></div>
Now, lets do this the right way eh?
PHP Code:
//If direction sent by $_GET use it, otherwise default to asc
$Direction = (isset($_GET['sortby']))? $_GET['sortby']: 'asc';
//Use it in our output
print "<div class=\"sortbtns\"><a href=\"{$_SERVER['PHP_SELF']}?sortby=year&sortby=$Direction\">Year</a></div> ";
/*
Now suppose its the default... you've output a link with 'asc'
You output the data by desc so you see change when you click the link
*** Direction in link is always opposite to what you output ***
*/
//Swap the direction so that it is the opposite direction of what we're outputting:
$Direction = ($Direction == 'asc')? 'desc': 'asc';
$SQL = "select * from <table> sort by <field> $Direction";