想要按cat_id
对价格进行排序,当从下拉类别中点击产品时,产品将在结果中打开.php页面,当单击从高到低时,它不会显示任何产品数据库。
功能.php
function getcategoryPro(){
if(isset($_GET['category'])){
$cat_id = $_GET['category'];
// SQL query to interact with info from our database
$get_category_pro = "SELECT * FROM products WHERE cat_id =$cat_id";
if (isset($_GET['sortby'])) {
$sortby = $_GET['sortby'];
// SQL query to interact with info from our database
if ($sortby == 'pricehilo') {
$get_category_pro = "SELECT * FROM products WHERE cat_id =$cat_id ORDER BY price DESC";
}
elseif ($sortby == 'pricelohi') {
$get_category_pro ="SELECT * FROM products WHERE cat_id =$cat_id ORDER BY price ASC";
}
}
$run_category_pro= mysql_query($get_category_pro);
$count = mysql_num_rows($run_category_pro);
if($count==0){
echo"<h2>no Products found in this category!";
}
$dynamicList = '<table border="0" cellpadding="2">';
while($row_products = mysql_fetch_array($run_category_pro)){
$id = $row_products["id"];
$product_name = $row_products["product_name"];
$price=$row_products["price"];
$date_added = strftime("%b %d, %Y",strtotime($row_products['date_added']));
if ($i % 9 == 0) { // if $i is divisible by our target number (in this case "3")
$dynamicList .=
'<tr><td><img style="padding:10px;/><a href="product.php?id=' . $id . '"><img
style="border: #000033 1px solid;" src="inventory_images/' . $id . '.jpg"
width="200" height="225" alt="' . $product_name . '"/><br/>
<width="150" align="left" valign="top" scope="col">' . $product_name . '<br/>
Rs. ' . $price . '<br/>
<a href="product.php?id=' . $id . '"><button>Buy Now</button></td>';
} else {
$dynamicList .= '<td><img style="padding:10px;/><a href="product.php?id=' . $id .
'"><img style="border: #000033 1px solid;" src="inventory_images/' . $id . '.jpg"
width="200" height="225" alt="' . $product_name . '"/><br/>
<width="150" align="left" valign="top" scope="col">' . $product_name . '<br/>
Rs. ' . $price . '<br/>
<a href="product.php?id=' . $id . '"><button>Buy Now</button></td>';
}
$i++;
}
echo $dynamicList;
$dynamicList .= '</tr></table>';
}
}
results.php
<?php
getcategoryPro();
?>
<p><a href="results.php?sortby=pricehilo">Price (Highest-Lowest)</a></p>
<p><a href="results.php?sortby=pricelohi">Price (Lowest-Highest)</a></p>
当从下拉菜单单击手机时,此链接随产品一起提供
results.php?category=1
当我们单击从高到低时,此链接与味精一起打开在此类别中找不到产品
results.php?sortby=pricelohi
它应该是:
results.php?category=1&sortby=pricelohi
<?php
getcategoryPro();
if (isset ($_GET['category']) && !empty($_GET['category'])) {
$cat_id = $_GET['category'];
} else {
// you decide
}
?>
<p><a href="results.php?category=<?= $cat_id ?>&sortby=pricehilo">Price (Highest-Lowest)</a></p>
<p><a href="results.php?category=<?= $cat_id ?>&sortby=pricelohi">Price (Lowest-Highest)</a></p>
只需将您的链接更改为<a href="results.php?category=<?php echo $_GET['category']; ?>&sortby=pricehilo">
即可。