Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-01-2013, 09:32 PM   PM User | #1
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
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.
SlayerACC is offline   Reply With Quote
Old 01-01-2013, 10:39 PM   PM User | #2
weir-07
New Coder

 
Join Date: Sep 2007
Posts: 98
Thanks: 25
Thanked 4 Times in 4 Posts
weir-07 is an unknown quantity at this point
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
weir-07 is offline   Reply With Quote
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,505
Thanks: 45
Thanked 439 Times in 428 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 wrap your code in [php] tags. It is a sticky topic 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
Old 01-02-2013, 09:20 PM   PM User | #4
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
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
SlayerACC is offline   Reply With Quote
Old 01-02-2013, 11:48 PM   PM User | #5
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,505
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by SlayerACC View Post
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?

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 - 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"
__________________
Please wrap your code in [php] tags. It is a sticky topic 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 11:52 PM..
tangoforce is offline   Reply With Quote
Old 01-03-2013, 03:37 AM   PM User | #6
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
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.
SlayerACC is offline   Reply With Quote
Old 01-03-2013, 02:22 PM   PM User | #7
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,505
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Because I'm a blithering idiot thats why it's not changing It's right in principle but just a bit mixed up

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
__________________
Please wrap your code in [php] tags. It is a sticky topic 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-03-2013 at 02:27 PM..
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
SlayerACC (01-03-2013)
Old 01-03-2013, 03:13 PM   PM User | #8
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
Now thats what I am talking about!!!


You are awesome ..


Works like a charm.


Thank you sooooo much!!


Slayer.
SlayerACC is offline   Reply With Quote
Old 01-03-2013, 03:31 PM   PM User | #9
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,505
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
No worries
__________________
Please wrap your code in [php] tags. It is a sticky topic 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.
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:02 AM.


Advertisement
Log in to turn off these ads.