点击高价到低价时应该改变

时间:2014-12-04 13:44:09

标签: php mysql

当从下拉类别产品中点击产品时,想要按cat_id排序价格 在results.php页面中打开,当点击从高到低时,它显示没有产品 数据库中。

function.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.&nbsp;' . $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.&nbsp;' . $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

当我们点击从高到低时,此链接将以msg打开 没有找到该类别的产品

results.php?sortby=pricelohi

它应该是:

results.php?category=1&sortby=pricelohi

2 个答案:

答案 0 :(得分:0)

只需将您的链接更改为<a href="results.php?category=<?php echo $_GET['category']; ?>&sortby=pricehilo">

答案 1 :(得分:0)

<?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>