2017-01-27 242 views
1

我現在正在購物車。我想要發生的是,當我點擊某個類別時,其下的所有產品將根據其類別ID進行顯示。如果有其他方式,請讓我知道。按類別顯示產品ID php&mysql

我用得到我的數據庫我的類別:

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> 

    "; 


    } 



} 

回答

0

所以基本上你每個類別都有一個按鈕,當你點擊它時,它會顯示一個帶有產品的菜單,按b排序y ID。我知道這可以通過下拉菜單完成。這個只是由CSS製作的。如果我弄錯了,我很抱歉,但請更具體地說明你想要什麼。

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