Gadgetster
Gadgetster

Reputation: 483

PHP Sorting query results after the search is submitted

How can I order the query results after the search has been done and the items are displayed on the screen? I want the user to be able to rearrange the order of results based on preference of price, data, etc..

$output ='';
    if(isset($_POST['search'])){
        $searchq = $_POST['search'];
        $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

        $query = mysql_query("SELECT * FROM `db_table` WHERE title LIKE '%$searchq%' OR description LIKE '%$searchq%' ORDER BY `date` ASC");
        $count = mysql_num_rows($query);
        if($count == 0){
            $output = 'No results found';
        }else{
            while($row = mysql_fetch_array($query)){
         //display divs

All help is much appreciated!

Upvotes: 0

Views: 280

Answers (3)

Andy  Gee
Andy Gee

Reputation: 3335

If you're using jquery this plugin is very effective and small http://tablesorter.com/docs/#Demo

Upvotes: 0

Adam
Adam

Reputation: 1371

You can sent some data by $_GET and then do something like this in query:

$query = mysql_query("SELECT * FROM `db_table` WHERE title LIKE '%$searchq%' OR description LIKE '%$searchq%' ORDER BY `".$_GET['orderby']."` ASC");

Of course You should secure code against SQL injections ;)

Upvotes: 0

Gromish
Gromish

Reputation: 199

After? You can do it in Javascript, manipulating the html.

There are many ways, many libraries. The first I found is that one:

https://datatables.net/

You can do it also without a library, but will be much harder.

Upvotes: 1

Related Questions