Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: Sort Button

  1. #1
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    228
    Thanks
    45
    Thanked 3 Times in 3 Posts

    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.

  • #2
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts
    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

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,092
    Thanks
    51
    Thanked 506 Times in 493 Posts
    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"
    Last edited by tangoforce; 01-02-2013 at 07:43 PM.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #4
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    228
    Thanks
    45
    Thanked 3 Times in 3 Posts
    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

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,092
    Thanks
    51
    Thanked 506 Times in 493 Posts
    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"
    Last edited by tangoforce; 01-02-2013 at 11:52 PM.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #6
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    228
    Thanks
    45
    Thanked 3 Times in 3 Posts
    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.

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,092
    Thanks
    51
    Thanked 506 Times in 493 Posts
    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
    Last edited by tangoforce; 01-03-2013 at 02:27 PM.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • Users who have thanked tangoforce for this post:

    SlayerACC (01-03-2013)

  • #8
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    228
    Thanks
    45
    Thanked 3 Times in 3 Posts
    Now thats what I am talking about!!!


    You are awesome ..


    Works like a charm.


    Thank you sooooo much!!


    Slayer.

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,092
    Thanks
    51
    Thanked 506 Times in 493 Posts
    No worries
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •