Thread: Sort Button
View Single Post
Old 01-02-2013, 07:35 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,665
Thanks: 45
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by SlayerACC View Post
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 View Post
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"
__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by tangoforce; 01-02-2013 at 07:43 PM..
tangoforce is offline   Reply With Quote