我正在尝试使用下拉框对我的产品进行排序。当我选择一个选项时,代码不会运行且产品不会改变位置。 iv设法通过使用不同的按钮对产品进行排序,但我认为下拉列表在网站上看起来会更好。
<?php
echo $sort = @$_GET['order'];
if (!empty($sort)) {
echo $query="SELECT * FROM products ORDER BY '".$sort."'";
} else {
echo $query="SELECT * FROM products order by " ;
}
?>
<form name="sort" action="" method="post">
<select name="order">
<option value="choose">Make A Selection</option>
<option value="price_asc">Price </option>
<option value="price_desc">Z-A</option>
<option value="name_asc">A-Z</option>
</select>
<input type="submit" value=" - Sort - " />
</form>
<?php
//Run the query.
$record_set = $connection->query($query);
while( $row = $record_set->fetch_assoc() ) {
echo '<div class="product">';
echo '<div class="product-content"><h3>'. $row['name'].'</h3>' .'</div>' . '<br />'.'<div class="product-thumb"><img src="/ISD assignment2/images/'. $row['imageName'].'"></div>'. '<div class="product-desc">'.$row['description']. '</div>'. '<br />' .'£'. number_format($row['price'], 2) . '<a href="../cart/cart.php?add=' . $row['productID'] . '"><p>Add</a></p>';
echo '</div>';
}
?>
答案 0 :(得分:0)
Two potential issues:
1) Your Form has no action. I believe you are trying to get it to refresh onto itself.
which would be:
action=" <?php echo $_SERVER['PHP_SELF'] ?>"
2) You are submitting via POST, yet using GET.
$sort = $_POST['order'];