【发布时间】:2017-06-13 04:00:50
【问题描述】:
我目前正在做一个购物车。我想要发生的是当我单击一个类别时,它下的所有产品都将根据其类别 ID 显示。如果有其他方法请告诉我。
我使用以下方法从我的数据库中获取我的类别:
function getCats(){
global $con;
$get_cats = "select * from categories";
$run_cats = mysqli_query($con, $get_cats);
while ($row_cats = mysqli_fetch_array($run_cats)){
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_title'];
echo "<li><a href='#' class= 'category' cid='$cat_id'>$cat_title</a></li>";
}
}
并在我的 index.php 中调用它
<?php
getPro();
?;
这是我的functions.php中的代码
if(isset($_POST["get_product"]))
{
$cid = $_POST["cat_id"];
$sql = "SELECT * from products WHERE product_cat = '$cid'";
$run_query = mysqli_query($con,$query) or die(mysqli_query($con));
while($row = mysqli_fetch_array($run_query))
{
$pro_id = $row['product_id'];
$pro_title = $row['product_title'];
$pro_cat = $row['product_cat'];
$pro_price = $row['product_price'];
$pro_image = $row['product_image'];
echo"
<div class='col-md-4'>
<div class='panel panel-info'>
<div class='panel-heading'>$pro_title </div>
<div class='panel-body'>
<img src='admin_area/product_images/$pro_image' style='width:100px; height:100px;'/>
</div>
<div class='panel-heading'>$pro_price
<button pid='$pro_id' style='float:right;' class='btn btn-danger btn-xs'>Add to cart</button>
</div>
</div>
</div>
";
}
}
【问题讨论】: