2014-12-04 69 views
0

想要按價格排序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

回答

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> 
+0

感謝兄弟的工作 – 2014-12-05 03:43:09

+0

@rahulJ請問您可以標記答案爲正確的答案嗎? :) – Forien 2014-12-05 07:36:33

+0

請檢查其他問題也inventory_list.php 類型 ​​

0

只要改變你的鏈接<a href="results.php?category=<?php echo $_GET['category']; ?>&sortby=pricehilo">