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

标签 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。我希望这是足够的代码来理解和帮助。

最佳答案

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

$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 端进行任何更改,查询将为您排序!

关于php - 显示产品列表中特定类别的产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21757033/

相关文章:

ssl - 为什么人们仅在其网站中使用 SSL 来保护选择性页面?

php - 如何使用非法名称访问此对象属性?

php - 警告 :mysql_connect()[function. mysql-connect]:[2002]

php - 如何使用数组值作为没有循环的键?

php - mysql案例然后无法识别的关键字和无法识别的 token

parsing - 如何将产品标题(非结构化)解析为结构化数据?

php白屏死机

php - 从表中删除一列,用 PHP MYSQL 更新另一列

mysql - 查询与字段的一个或多个条件匹配的行

ruby-on-rails - ROR的电子商务 gem ?