我正在尝试在php页面中显示具有相同category
的所有产品。
产品保存在mysql数据库中!
在我的标题中,我有以下代码,它将在header.php
中显示/显示mysql数据库中的类别:
<?php
// Run a select query to get my letest 6 items
// Connect to the MySQL database
include "config/connect.php";
$dynamicList = "";
$sql = "SELECT DISTINCT category FROM products";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$category = $row["category"];
$dynamicList .= '<li class="categories">
<div>
<div id="lookbookmedia" class="column"> <a href="product_list.php?category=' . $category . '">' . $category . '</a></div>
</div>
</li><li><a href="product_list.php?category=' . $category . '">' . $category . '</a></li>';
}
} else {
$dynamicList = "NO Cats Yet!";
}
?>
目前我在我的product_list.php
页面中使用了以下代码,但这只会显示一个产品,即mysql数据库中的LAST
,无论我在标题中点击哪个类别!
<?php
if (isset($_GET['category'])) {
// Run a select query to get my letest 6 items
// Connect to the MySQL database
include "config/connect.php";
$category = preg_replace('#[^0-9]#i', '', $_GET['category']);
$dynamicList2 = "";
$sql = "SELECT * FROM products WHERE category= category LIMIT 25" ;
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$category = $row["category"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList2 = '<table class="border" style="float:left; margin-left:43px; text-align:center; background-color:#e3e3e3; margin-bottom:10px;" width="300" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><a href="product.php?id=' . $id . '"><img src="inventory_images/' . $id . '.jpg" width="300" height="450" alt="' . $product_name . '" longdesc="' . $product_name . '" /></a></td>
</tr>
<tr>
<td style="color:#000; font-weight:bold;">' . $product_name . '</td>
</tr>
<tr>
<td style="color:#000; font-weight:bold;">Price: £' . $price . '</td>
</tr>
<tr>
<td><a href="product.php?id=' . $id . '">View Product Details</a></td>
</tr>
<tr>
<td> </td>
</tr>
</table>';
}
}
} else {
$dynamicList2 = "We have no products listed in our store yet";
}
?>
我不明白我哪里错了!
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
你错了:
$category = preg_replace('#[^0-9]#i', '', $_GET['category']);
$dynamicList2 = "";
$sql = "SELECT * FROM products WHERE category='".$category."' LIMIT 25" ;
更改变量$category