CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Sort Button (http://www.codingforums.com/showthread.php?t=285122)

SlayerACC 01-01-2013 09:32 PM

Sort Button
 
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?


current button:
PHP Code:

<div class="sortbtns\"><a href='{$_SERVER['PHP_SELF']}?sortby=year'>Year</a></div> 


Thanks.

weir-07 01-01-2013 10:39 PM

Before page content and before your DB query.
PHP Code:

if ($_GET['sortby']) {
$sql_string="ASC"; } else { $sql_string="DESC"; } 

In the SQL query of your script;
PHP Code:

$query="SELECT * FROM table_name"

Change to...
PHP Code:

$query="SELECT * FROM table_name SORT BY ".$sql_string


tangoforce 01-02-2013 07:35 PM

Quote:

Originally Posted by SlayerACC (Post 1303454)
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 (Post 1303463)
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 :eek: :thumbsup: :rolleyes:

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"


SlayerACC 01-02-2013 09:20 PM

Hey Tangoforce,

This is exactly what I am looking for..

I have tested the code you put in place and it never seems to change from 'ASC'


Any thoughts to it.?

Thanks again.

Slayer

tangoforce 01-02-2013 11:48 PM

Quote:

Originally Posted by SlayerACC (Post 1303646)
I have tested the code you put in place and it never seems to change from 'ASC'

What never changes? The link or the database query? :o

Also having looked at the code, you'll need to change the sortby name of the $_GET array (and the link) to something else - sorry, didn't realise it was already in use for the year :o - infact thats probably the actual problem so try this instead:

(Note that sortby is now sortdir)

PHP Code:

//If direction sent by $_GET use it, otherwise default to asc
$Direction = (isset($_GET['sortdir']))? $_GET['sortdir']: 'asc';

//Use it in our output
print "<div class=\"sortbtns\"><a href=\"{$_SERVER['PHP_SELF']}?sortby=year&sortdir=$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"


SlayerACC 01-03-2013 03:37 AM

Hey Tango,


I think it is the link that never changes?

PHP Code:

<?php

//If direction sent by $_GET use it, otherwise default to asc
$Direction = (isset($_GET['sortdir']))? $_GET['sortdir']: 'asc';

//Use it in our output
print "<div class=\"sortbtns\"><a href=\"{$_SERVER['PHP_SELF']}?sortby=year&sortdir=$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";  

echo 
$Direction;
?>

I run it like this to see if the link changes or the $Direction and it never does.?

Sorry to be a pain.. maybe I am jut not understanding.

Thanks again..


Slayer.

tangoforce 01-03-2013 02:22 PM

Because I'm a blithering idiot thats why it's not changing :p It's right in principle but just a bit mixed up :o

PHP Code:

 <?php
//If direction sent by $_GET use it, otherwise default to asc
$Direction = (isset($_GET['sortdir']))? $_GET['sortdir']: 'asc';

//Change link to it's opposite to what it was:
$Direction = ($Direction == 'asc')? 'desc''asc';

//Use it in our output
print "<div class=\"sortbtns\"><a href=\"{$_SERVER['PHP_SELF']}?sortby=year&sortdir=$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";  

echo 
$SQL;
?>

There is just one change there. I've swapped the direction just before putting it into the link. This means that when the direction is taken from the get array it will be changed to the opposite of what it is, put into the link, and then swapped over again to match the get array for the SQL. No sanitization of it needed because the ternary sets the direction for the sql not actual user input.

Edit:
This time I've even tested it in my own browser to be sure it works :)

SlayerACC 01-03-2013 03:13 PM

Now thats what I am talking about!!!


You are awesome ..


Works like a charm.


Thank you sooooo much!!


Slayer.

tangoforce 01-03-2013 03:31 PM

No worries :thumbsup:


All times are GMT +1. The time now is 07:16 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.