I am basically creating a website and it is a site that sells mobile phones. I need to find a way using PHP to create a drop-down box that can sort the phones by model, brand, screen size etc. (Fields already in my SQL database). What is the simplest way of going about this? I am quite new to PHP and not too sure how to go about it.
问问题
3820 次
2 回答
2
您在 MySQL 查询中处理排序:
SELECT * FROM <<table>> ORDER BY 'Brand' DESC
然后在您的页面中调用结果;您无法通过 javascript(用户端)对查询重新排序,因为该查询称为服务器端。
于 2013-01-15T17:09:24.743 回答
1
您可以使用$_GET
变量来实现这一点。像这样的东西
<?php
// Let's say that my products category is a $_GET variable
$category_id = isset($_GET['category_id']) && is_numeric($_GET['category_id']) ? $_GET['category_id'] : 1; // where 1 is a default category to show!
?>
<select onchange="if(this.value != '') document.location = '/my-phones-category.php?category_id=<?php echo $category_id; ?>&order_by=' + this.value">
<option value="">Choose an option</option>
<option value="model"<?php if(isset($_GET['order_by']) && $_GET['order_by'] == 'model') echo ' selected="selected"'; ?>>Model</option>
<option value="brand"<?php if(isset($_GET['order_by']) && $_GET['order_by'] == 'brand') echo ' selected="selected"'; ?>>Brand</option>
</select>
然后我们必须看看如何进行查询以添加正确的订单
<?php
switch($_GET['order_by']) {
case 'model':
$order_by = " ORDER BY products.product_model";
break;
case 'brand':
$order_by = " ORDER BY products.product_brand";
break;
default:
$order_by = " ORDER BY products.product_price";
}
// Now we have the order and we can make the query
$sql = $mysqli->query("SELECT product_name, product_price
FROM products
WHERE category_id='" . $category_id . "'
$order_by");
?>
祝你好运!
于 2013-01-15T17:13:56.450 回答