Quick Search:
 PHP Code: Handy SORT BY box Jump to:  
Category: >> PHP Code >> Handy SORT BY box  

<< lastnext >>

Snippet Name: Handy SORT BY box

Description: This code produces a sort box for any number of arbitrary criteria on any type of data you have in a database. The hidden 'param1' and 'param2' variables are arbitrary, for example they could be a search query and category id.

This also includes a javascript auto-submit so users don't need to click a "Go" or "Sort" button after making their selection.

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: March 01st, 2009

$selected = ARRAY();
$orderby = $_GET[orderby];
IF(!$orderby) { $orderby = 'price_asc'; }
IF($orderby == 'price_asc') 
    $orderby_query = "order by price asc";
ELSE IF($orderby == 'price_desc')
     $orderby_query = "order by price desc";
ELSE IF($orderby == 'name')
     $orderby_query = "order by name";
ELSE { UNSET($orderby); }
// If $orderby was valid set the selected sort option for the form.
     $selected[$orderby] = 'selected';
// Now run your SQL query with the $orderby_query variable.  Ex:
$query = "select * from products $orderby_query";
// SQL code goes here..
Sort by
<form method=get style="display: inline;" name='orderby_form'>
<input type=hidden name='param1' value="<?PHP PRINT $param1; ?>">
<input type=hidden name='param2' value="<?PHP PRINT $param2; ?>">
<select name=orderby onChange="orderby_form.submit();">
<option value='name' <?PHP PRINT $selected[$orderby]; ?>>Name</option>
<option value='price_asc' <?PHP PRINT $selected[$orderby]; ?>>Price (Low - High)</option>
<option value='price_desc' <?PHP PRINT $selected[$orderby]; ?>>Price (High - Low)</option>

   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org