I try to sort alphabetically products by price in the dropdwon menu, like in this question solution:
I have a db table products with product_id, product_price.
I get the id in the url but is not showing any product.
<select name="order_by" onchange="if(this.value != '') document.location ='sort_by.php?product_id=<?php echo $product_id; ?>&order_by=' + this.value">
<option value="">Choose</option>
<option value="asc" <?php if(isset($_GET['order_by']) && $_GET['order_by'] == 'asc') echo ' selected="selected"'; ?>>Ascendent</option>
<option value="desc" <?php if(isset($_GET['order_by']) && $_GET['order_by'] == 'desc') echo ' selected="selected"'; ?>>Descendent</option>
</select>
The function:
switch($_GET['order_by']) {
case 'asc':
$order_by = " ORDER BY product_price ASC";
break;
case 'desc':
$order_by = " ORDER BY product_price DESC";
break;
default:
$order_by = " ORDER BY product_price";
}
$query = query("SELECT * FROM products WHERE product_id =" . escape_string($_GET['product_id']) . ". $order_by");
Thank you for your help!