Sorting Database Results in PHP

Ask a Question related to Dreamweaver AppDev, Design and Development.

  1. #1

    Default Sorting Database Results in PHP

    I am working on putting a drop down menu on my page which allows the user to
    change the order of which he/she wants to view the database results, for
    example by date, price, etc...

    I guess where I run into trouble would be the SQL query. I understand the
    statement ORDER BY price ASD or DESC. But how can I rewritre my statement so
    the user has a choice to change the order of things??

    Any help would be greatly appreciated!

    TheRapidGroup Guest

  2. Similar Questions and Discussions

    1. Database Results
      Hi I have created a results page so that when a users types something in my search field on the web the results page shows a list of products....
    2. Why Does Sorting Require Another Call To The Database?
      I hope someone can help with this. Every example I've seen, and every example I've written, where I fill a datagrid from a recordset I get from...
    3. Database results error
      I've originally posted this in the front page extensions page, but was told to stop by here. Here's a copy of what we said. Any ideas on how...
    4. Sorting a list held in a database
      Hi all, I need to build a list of text items. Using asp I want to add text items to a list and then sort the list in the order I wish. The...
    5. scoring/sorting db search results based on score
      looking for code snippets or links to examples of the following; - Have a database with multiple fields that will be searched against (happens to...
  3. #2

    Default Re: Sorting Database Results in PHP

    "TheRapidGroup" <webforumsuser@macromedia.com> wrote in message
    news:cvq5lp$bju$1@forums.macromedia.com...
    > I am working on putting a drop down menu on my page which allows the user
    to
    > change the order of which he/she wants to view the database results, for
    > example by date, price, etc...
    >
    > I guess where I run into trouble would be the SQL query. I understand
    the
    > statement ORDER BY price ASD or DESC. But how can I rewritre my statement
    so
    > the user has a choice to change the order of things??
    >
    > Any help would be greatly appreciated!
    If you're using a drop-down menu, you could do something like this:

    <?php
    $sortorder = "ASC";
    $sortfield = "Price";

    if(isset($_POST["sortorder"])) {
    $sortorder = $_POST["sortorder"];
    }
    if(isset($_POST["sortfield"])) {
    $sortfield= $_POST["sortfield"];
    }

    then in your SQL statement:

    "SELECT blah FROM mytable
    ORDER BY $sortfield $sortorder"

    Your dropdowns would look something like this:

    <select name="sortorder">
    <option value="ASC">Ascending</option>
    <option value="DESC">Descending</option>
    </select>
    <select name="sortfield">
    <option value="Price">Price</option>
    <option value="field2">Field2</option>
    etc.
    </select>

    I also have a commercial extension for sorting tables that you may find
    suits your needs, as it automates things:
    [url]http://www.tom-muck.com/extensions/help/sortrepeatregion/[/url]


    --
    -------------------------------------------
    Tom Muck
    co-author Dreamweaver MX 2004: The Complete Reference
    [url]http://www.tom-muck.com/[/url]

    Extending Knowledge, Daily
    [url]http://www.CommunityMX.com/[/url]


    Tom Muck Guest

  4. #3

    Default Re: Sorting Database Results in PHP

    Thanks for replying, I made a few minor changes to the last few lines of code
    only because I was getting "File Not Found" error... but I want to show you
    what I have done thus far:

    PHP
    <?php
    $sortorder = "ASC";
    $sortfield = "Price";

    if(isset($_POST["sortorder"])) {
    $sortorder = $_POST["sortorder"];
    }
    if(isset($_POST["sortfield"])) {
    $sortfield= $_POST["sortfield"];
    }

    SQL QUERY:
    FROM clocks ORDER BY $sortfield $sortorder

    MENUS
    <select name="menu1" onChange="MM_jumpMenu('parent',this,0)">
    <select name="sortorder">
    <option value="?sortorder=ASC">Ascending</option>
    <option value="?sortorder=DSC">Descending</option>
    </select>
    <select name="menu2" onChange="MM_jumpMenu('parent',this,0)">
    <select name="sortfield">
    <option value="?sortfield=price">Price</option>
    <option value="?sortfield=item">Item</option>
    </select>

    Now when I select one of these two items, it would appear to be executing
    except nothing on the page changes in terms of sorting.... can you see where I
    am going wrong?


    TheRapidGroup Guest

  5. #4

    Default Re: Sorting Database Results in PHP

    .oO(Tom Muck)
    >$sortorder = "ASC";
    >$sortfield = "Price";
    >
    >if(isset($_POST["sortorder"])) {
    > $sortorder = $_POST["sortorder"];
    >}
    >if(isset($_POST["sortfield"])) {
    > $sortfield= $_POST["sortfield"];
    >}
    >
    >then in your SQL statement:
    >
    >"SELECT blah FROM mytable
    >ORDER BY $sortfield $sortorder"
    Ever heard of SQL injection?

    Micha
    Michael Fesser Guest

  6. #5

    Default Re: Sorting Database Results in PHP

    > Now when I select one of these two items, it would appear to be executing
    > except nothing on the page changes in terms of sorting.... can you see
    where I
    > am going wrong?
    You're using query strings instead of form variables, so you have to change
    your POSTs to GETs. Also, Michael brings up the point of SQL injection,
    which you should also concern yourself with, by using something like this
    instead:

    if(isset($_GET["sortorder"])) {
    $sortorder = (!get_magic_quotes_gpc()) ?
    addslashes($_GET["sortorder"]; ) : $_GET["sortorder"];;
    }


    Tom


    Tom Muck Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139