Sort search results


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?

$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:

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

Thanks in advance

Not sure if this is the kind of thing you are after:

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.

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.

<script type="text/javascript" src="sorttable.js"></script>
 <style type="text/css"><!--
table.sortable thead {
    font-weight: bold;
    cursor: default;

<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></td>     <td>$50.00</td>     <td></td> </tr> <tr>     <td>Bach</td>     <td>Frank</td>     <td></td>     <td>$50.00</td>     <td></td> </tr> </tbody> </table> 

Thanks again

  1. 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?


Nope. I’ll signal this post to be moved to the JS forum.

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.