SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2003
    Location
    uk
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy sort search results

    Hi,

    I want to sort the database search results in php. Basically, the results are returned on a php page and need to be sorted either by columns. I managed to display the results on the page witht he code below and it works, but now i need to add the sorting bit, but i am stuck with it. I dont want to fetch the data from the database again but just to manipulate the existing entries. how can this be achieved in php?


    Code:
    $sql .= 'SELECT    * FROM dealstable  WHERE  search  like "%' . $dest . '%" ';
    
    
    $result = mysql_query($sql) or die("Query error: ".mysql_errno().": ".mysql_error());
    
    
    while ($row = mysql_fetch_array($result))
    {
    
    print "<tr>"
    ;
    
    
    print "<td><img  height='60' src='" . $row["id"] . "'></td>"
    ;
    
    print "<td align='center'>" . $row["deals"] . "</td>"
    ;
    I want to add the sort option on the search results so that users can filter

    something like:
    Code:
    Sort  by 
    <select name="sort" " id="sort" style="width:150px;">
    <option value="lowprice">Lowest Price Asc</option>
    <option value="highprice">Highest Price Desc</option>
    etc


    Thanks in advance

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,499
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    When the user chooses to sort the data, you have several options:
    1) The form is sent to the server and a PHP script gets the data from the database again, this time sorted
    2) You use AJAX to send a request to the server and a PHP script gets the data from the database again, this time sorted
    3) You use JS to sort the data

    You cannot use PHP to sort the data without fetching it from the database again. Well, maybe you could but you would have to send all the data back to the server, which is really a non-solution.

  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2003
    Location
    uk
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    3) You use JS to sort the data. This sounds a feasable option, but i need some guidance on how to manipualte the returned results. using something like Onclick event? Do you know any examples I can follow similar to my scenario?

    thanks

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,499
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Nope. I'll signal this post to be moved to the JS forum.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Not sure if this is the kind of thing you are after:

    http://www.greepit.com/2010/10/flexi...g-tablesorter/

    I've spotted several solutions involving jQuery which seem to do this client-side table sorting rather nicely - presupposes you are displaying in a properly constructed table of course.

  6. #6
    SitePoint Evangelist
    Join Date
    Dec 2003
    Location
    uk
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok thanks. I found a solution similar to yours and it does the trick, but the only problem is when the page is loaded the first time, there is no indication that the columns are sortable. its only if the user presses the colum header, then they become sortable, which is not intuitive. is there a way of making a visual display on the columns that they are sortable when the page is loaded before even clicking on the column name?


    I attached the jaascript file used and below is the example page.


    [code]
    <head>
    <script type="text/javascript" src="sorttable.js"></script>
    <style type="text/css"><!--
    table.sortable thead {
    background-color:red;
    color:blue;
    font-weight: bold;
    cursor: default;
    }
    </head>

    <body>
    <table class = 'sortable' border='1' cellpadding = '0' cellspacing='0'>" <thead> <tr> <th>Last Name</th> <th>First Name</th> <th>Email</th> <th>Due</th> <th>Web Site</th> </tr> </thead> <tbody> <tr> <td>Smith</td> <td>John</td> <td>jsmith@gmail.com</td> <td>$50.00</td> <td>http://www.jsmith.com</td> </tr> <tr> <td>Bach</td> <td>Frank</td> <td>fbach@yahoo.com</td> <td>$50.00</td> <td>http://www.frank.com</td> </tr> </tbody> </table>
    </body>
    [code]

    Thanks again
    Attached Files Attached Files


Bookmarks

Posting Permissions

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