简体   繁体   English

按类别ID php和mysql显示产品

[英]Display products by category ID php&mysql

I`m currently doing a shopping cart. 我正在购物车。 What I want to happen is when I click a category all the products under it will be displayed according to its category id. 我想要发生的是当我点击一个类别时,它下面的所有产品将根据其类别ID显示。 If there is other way please let me know. 如果还有其他方式,请告诉我。

I get my categories from my database using: 我从我的数据库中获取我的类别:

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>";

}

}

and call it in my index.php 并在我的index.php中调用它

<?php 
getPro();
 ?;

This is my code in my functions.php 这是我的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>

    ";


    } 



}

So basically you a button for each categorie, and when you click it, it shows a little menu with the products in it, sorted by ID. 所以基本上你是每个类别的按钮,当你点击它时,它会显示一个包含产品的小菜单,按ID排序。 I know this can be done by a dropdown menu. 我知道这可以通过下拉菜单完成。 This one is only made by CSS. 这个只由CSS制作。 I'm sorry if I get you wrong, but then please be more specific on what you want. 如果我弄错了,我很抱歉,但请更具体地说明你想要什么。

<style>
.dropdown {
position: relative;
display: inline-block;`enter code here`
}

.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
padding: 12px 16px;
z-index: 1;
}

.dropdown:hover .dropdown-content {
display: block;
}
</style>

<div class="dropdown">
<span>Mouse over me</span>
<div class="dropdown-content">
<p>Hello World!</p>
</div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM