Quick Search:
Oracle PL/SQL Code Library
Find Or Post Oracle Jobs
Oracle Discussion & Chat
 PHP Code: Handy SORT BY box Jump to:  
Category: >> PHP Code >> Handy SORT BY box Bookmark and Share

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

Oracle Magazine
and Oracle White Papers

SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 25 users online    © 2009 psoug.org

Forgot your password?