gpt4 book ai didi

php - 在 PHP/Mysql 中显示所有具有相同类别名称的产品?

转载 作者:行者123 更新时间:2023-11-30 00:52:36 25 4
gpt4 key购买 nike

我正在尝试在 php 页面中显示具有相同类别的所有产品。

产品保存在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>&nbsp;</td>
</tr>
</table>';
}
}
} else {
$dynamicList2 = "We have no products listed in our store yet";
}

?>

我不明白我哪里错了!

有人可以帮我解决这个问题吗?

最佳答案

你错了:

$category = preg_replace('#[^0-9]#i', '', $_GET['category']);

$dynamicList2 = "";
$sql = "SELECT * FROM products WHERE category='".$category."' LIMIT 25" ;

更改变量$category的文字category

关于php - 在 PHP/Mysql 中显示所有具有相同类别名称的产品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20830620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com