0

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!

Gadgetster
  • 473
  • 3
  • 12
  • 33
  • Seems like a JavaScript solution may be the way. – Ja͢ck Feb 20 '14 at 07:34
  • I thought so but not sure how to do that with javascript. Could you give me an example? – Gadgetster Feb 20 '14 at 07:34
  • What you can do is, the place where the user selects the sort preference, pass it as a `GET` variable to the same page, and in your query, check if the `GET` variable exists, if yes use that order or use maybe `date` as default. – AyB Feb 20 '14 at 07:36
  • Depending on how you present this data. [Datatables](https://datatables.net/) may be a good Javascript solution for you. You will need to learn how to format your database query into JSON but it might be worth it [Server Side Data](https://datatables.net/release-datatables/examples/server_side/server_side.html) – Newbi3 Feb 20 '14 at 07:37
  • what about datatables ?, that would be very easy to implement http://datatables.net/ – Sanoob Feb 20 '14 at 07:37
  • Is this helpful? http://stackoverflow.com/questions/14125166/best-way-of-storing-user-settings-in-mysql – Vijayakumar Selvaraj Feb 20 '14 at 07:46
  • Is it possible to use this datatables in my own design rather than actually have a table with headings and arrows like that? – Gadgetster Feb 20 '14 at 07:47
  • @VijayakumarSelvaraj not really as this will save the settings yes but what if I search first without any settings saved, then I want to change the order of the results? Right now it will throw me back to the home page with the main items rather than the search results – Gadgetster Feb 20 '14 at 07:49
  • @Gadgester users mean? registered/loggedin users or guest? – Vijayakumar Selvaraj Feb 20 '14 at 07:53
  • Well only registered users will be able to save settings but assuming no one saved any settings yet and they search first then set up their filter settings, the search results redirect to the homepage once the submit button is clicked for the filters – Gadgetster Feb 20 '14 at 08:04
  • any idea how to rearrange the results in a selected order after the results are displayed? – Gadgetster Feb 20 '14 at 21:23

3 Answers3

1

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.

Gromish
  • 189
  • 2
  • 9
  • thing is I don't want it to be restricted to a table format as my results show up in divs and I have radio buttons for the order changing. Is there a way to implement this library to do that? – Gadgetster Feb 20 '14 at 07:43
  • Maybe is a wrong choice to display data table in divs, but it's a guess. Otherwise You can reload the page, changing the query. – Gromish Feb 20 '14 at 07:57
  • Well I have posts and that are all styled and I wanted them to show up one after another nicely rather than a structured table right – Gadgetster Feb 20 '14 at 08:06
0

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 ;)

Adam
  • 1,371
  • 2
  • 11
  • 12
  • This may help you with that http://stackoverflow.com/questions/7831712/jquery-sort-divs-by-innerhtml-of-children – Andy Gee Feb 21 '14 at 10:17
0

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

Andy Gee
  • 3,149
  • 2
  • 29
  • 44
  • any way of doing this without using the table? i have divs and radio buttons that change the order of the divs by using a query – Gadgetster Feb 20 '14 at 21:23