显示产品列表中特定类别的产品

时间:2014-02-13 14:29:34

标签: php mysql e-commerce

我只想询问如何仅在产品列表中显示特定类别?

<?php

include "storescripts/connect_to_mysql.php";
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 15");
$productCount = mysql_num_rows($sql); // count the output amount
$i = 0;

$dynamicList = '<table  border="1" bordercolor="#d6d6d6" cellpadding="10"         
cellspacing="0" style="border-collapse:collapse;">';
while($row = mysql_fetch_array($sql)){ 
     $id = $row["id"];
     $brand = $row["brand"];
     $product_name = $row["product_name"];
     $pdetails = $row["pdetails"];
     $details = $row["details"];
     $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
     if ($i % 3 == 0) { // if $i is divisible by our target number (in this case "3")
     $dynamicList .= '<tr><td><a href="product.php?id=' . $id . '" title="' . $pdetails . '"><img src="inventory_images/' . $id . '.jpg" height="280" border="0" ><br><br>' . $brand . '<br>' . $product_name . '</a></td>';
} else {
$dynamicList .= '<td><a href="product.php?id=' . $id . '" title="' . $pdetails . '"><img src="inventory_images/' . $id . '.jpg" height="280" border="0" ><br><br>' . $brand . '<br>' . $product_name . '</a></td>';
}
$i++;

}
$dynamicList .= '</tr></table>';

?>

我要展示的类别为$comment,其值为Best Seller。我希望这是足够的代码来理解和帮助。

1 个答案:

答案 0 :(得分:0)

如果您只想展示所有畅销书,那么您可以更改查询

$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 15");

$sql = mysql_query("SELECT * FROM products 
where `comment` = 'Best Seller'
ORDER BY date_added DESC LIMIT 15");

以上内容将获取所有畅销产品。

您不需要在PHP端更改任何内容,查询将为您排序!