Handy SORT BY box+No SQL INjection
کد PHP:
<?PHP
$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.
IF($orderby)
{
$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>
</select>
</form>