我該如何排序我用MYSQL過濾的類別? 例如,我想獲得最便宜的帽子。我需要做什麼?我怎樣才能排序我用MYSQL過濾的類別?
if(isset($_GET['category'])){
$category = $_GET['category'];
$sql = "SELECT * FROM store WHERE category='$category'";}else{
$sql = "SELECT id,title,image FROM store";
}
if(isset($_GET['category'],$_GET['value'])){
$currentPage = $_GET['category'];
$value = $_GET['value'];
$sql = "SELECT * FROM store WHERE category='$category'ORDER BY $value";
}
if(isset($_GET['value'])) {
if($_GET['value'] == 'price') {
// query to get all by price
$sql = "SELECT * FROM store ORDER BY price";
}
elseif($_GET['value'] == 'title') {
// query to get all by title
$sql = "SELECT * FROM store ORDER BY title";
} elseif($_GET['value']== 'date') {
// query to get all by date added
$sql = "SELECT * FROM store ORDER BY date_added";
}
`
<div id="filters">
\t \t <a href="stores.php">All</a> \t
<a href="stores.php?category=t-shirt">T-Shirt</a>
<a href="stores.php?category=mug">Mug</a>
<a href="stores.php?category=hat">Hat</a>
\t </div>
\t <label for="sort">Sort By</label>
\t <select name="sort">
\t <option value="price">Price: Low to High</option>
\t <option value="title">Title: A-Z</option>
\t <option value="date">Newest</option>
\t </select>
沒有我想創建一個條件語句,我可以在同一時間對其進行過濾和排序,例如SELECT * FROM store ORDER BY price WHERE c ategory = $ category與我的條件聲明我只能排序所有的項目。我無法同時過濾和排序。 –