【问题标题】:show all the products that have same category name in PHP/Mysql?在 PHP/Mysql 中显示所有具有相同类别名称的产品?
【发布时间】:2013-12-30 06:11:54
【问题描述】:

我正在尝试在一个 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>&nbsp;</td>
  </tr>
</table>';
    }
}
} else {
    $dynamicList2 = "We have no products listed in our store yet";
}

?>

我不明白我哪里错了!

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

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    你错了:

    $category = preg_replace('#[^0-9]#i', '', $_GET['category']);
    
    $dynamicList2 = "";
    $sql = "SELECT * FROM products WHERE category='".$category."' LIMIT 25" ;
    

    更改变量$category的文字类别

    【讨论】:

    • 不幸的是,这不起作用。你的建议是单品也消失了!所以页面上什么都没有显示!
    • 类别存储为数字还是字符串?
    • 对不起,我应该在我的问题中这么说。类别作为字符串保存为products 表中的列。即,鞋子、靴子的类别在products 表的category 列中保存为鞋子、靴子。
    • 我认为您的 while 循环也是错误的,因为您在每次迭代中都覆盖了 $dynamicList2。试试$dynamicList2 .=
    猜你喜欢
    • 2018-09-09
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多