我需要将此类别和子类别的所有产品带到最终级别

时间:2018-04-30 10:39:38

标签: php database function mysqli

我需要为所有类别和子类别提供所有产品。它是如何工作的以及这段代码有什么问题?

这是一个我需要纠正的功能,我将把它放在分页代码和jQuery代码中,过滤相应的价格低和最新的最新产品

<?php
            function listproducts ($category_img=filter_var($_GET['category'],FILTER_SANITIZE_NUMBER_INT)){
                global $conn;
                $sql_cat_name="select * from categories where cat_id='$category_img' or cat_id in (select cat_id from categories where cat_parent_id='$category_img')";
                $query_cat_name=mysqli_query($conn,$sql_cat_name);
                while($result_cat_name=mysqli_fetch_array($query_cat_name)){
                    $ref_id=$result_cat_name['cat_id'];
                    $show_all_pro="select * from products where product_category='$ref_id'";
                    $query_show_all=mysqli_query($conn,$show_all_pro);
                    while($result_show_all=mysqli_fetch_array($query_show_all)){
                ?>
                <!-- 12 product.-->
            <div class="category-product-inner wow fadeInUp">
              <div class="products">
                <div class="product-list product">
                  <div class="row product-list-row">
                    <div class="col col-sm-4 col-lg-4">
                      <div class="product-image">
                        <div class="image"> <img src="assets/images/products/p3.jpg" alt=""> </div>
                      </div>
                      <!-- /.product-image --> 
                    </div>
                    <!-- /.col -->
                    <div class="col col-sm-8 col-lg-8">
                      <div class="product-info">
                        <h3 class="name"><a href="detail.html">Floral Print Buttoned</a></h3>
                        <div class="rating rateit-small"></div>
                        <div class="product-price"> <span class="price"> $450.99 </span> <span class="price-before-discount">$ 800</span> </div>
                        <!-- /.product-price -->
                        <div class="description m-t-10">Suspendisse posuere arcu diam, id accumsan eros pharetra ac. Nulla enim risus, facilisis bibendum gravida eget, lacinia id purus. Suspendisse posuere arcu diam, id accumsan eros pharetra ac. Nulla enim risus, facilisis bibendum gravida eget.</div>
                        <div class="cart clearfix animate-effect">
                          <div class="action">
                            <ul class="list-unstyled">
                              <li class="add-cart-button btn-group">
                                <button class="btn btn-primary icon" data-toggle="dropdown" type="button"> <i class="fa fa-shopping-cart"></i> </button>
                                <button class="btn btn-primary cart-btn" type="button">Add to cart</button>
                              </li>
                              <li class="lnk wishlist"> <a class="add-to-cart" href="detail.html" title="Wishlist"> <i class="icon fa fa-heart"></i> </a> </li>
                              <li class="lnk"> <a class="add-to-cart" href="detail.html" title="Compare"> <i class="fa fa-signal"></i> </a> </li>
                            </ul>
                          </div>
                          <!-- /.action --> 
                        </div>
                        <!-- /.cart --> 
                      </div>
                      <!-- /.product-info --> 
                    </div>
                    <!-- /.col --> 
                  </div>
                  <!-- /.product-list-row -->
                  <div class="tag new"><span>new</span></div>
                </div>
                <!-- /.product-list --> 
              </div>
              <!-- /.products --> 
            </div>
            <!-- /.category-product-inner -->
                <?
                    }
                    listproducts ($result_cat_name['cat_id']);
                }
            }
            echo listproducts();
        ?>

这是我的表需要从中带来的

分类 enter image description here

产品 enter image description here

1 个答案:

答案 0 :(得分:1)

以这种方式显示html代码仅适用于函数外部。 如果您真的想这样做,则需要将html代码放入echo,这样:

<?php
function listproducts ($category_img=filter_var($_GET['category'],FILTER_SANITIZE_NUMBER_INT)){
    global $conn;
    $sql_cat_name="select * from categories where cat_id='$category_img' or cat_id in (select cat_id from categories where cat_parent_id='$category_img')";
    $query_cat_name=mysqli_query($conn,$sql_cat_name);
    while($result_cat_name=mysqli_fetch_array($query_cat_name)){
        $ref_id=$result_cat_name['cat_id'];
        $show_all_pro="select * from products where product_category='$ref_id'";
        $query_show_all=mysqli_query($conn,$show_all_pro);
        while($result_show_all=mysqli_fetch_array($query_show_all)){
            echo '
            <!-- 12 product.-->
            <div class="category-product-inner wow fadeInUp">
                <div class="products">
                    <div class="product-list product">
                        <div class="row product-list-row">
                            <div class="col col-sm-4 col-lg-4">
                                <div class="product-image">
                                    <div class="image"> <img src="assets/images/products/p3.jpg" alt=""> </div>
                                </div>
                                <!-- /.product-image -->
                            </div>
                            <!-- /.col -->
                            <div class="col col-sm-8 col-lg-8">
                                <div class="product-info">
                                    <h3 class="name"><a href="detail.html">Floral Print Buttoned</a></h3>
                                    <div class="rating rateit-small"></div>
                                    <div class="product-price"> <span class="price"> $450.99 </span> <span class="price-before-discount">$ 800</span> </div>
                                    <!-- /.product-price -->
                                    <div class="description m-t-10">Suspendisse posuere arcu diam, id accumsan eros pharetra ac. Nulla enim risus, facilisis bibendum gravida eget, lacinia id purus. Suspendisse posuere arcu diam, id accumsan eros pharetra ac. Nulla enim risus, facilisis bibendum gravida eget.</div>
                                    <div class="cart clearfix animate-effect">
                                        <div class="action">
                                            <ul class="list-unstyled">
                                                <li class="add-cart-button btn-group">
                                                    <button class="btn btn-primary icon" data-toggle="dropdown" type="button"> <i class="fa fa-shopping-cart"></i> </button>
                                                    <button class="btn btn-primary cart-btn" type="button">Add to cart</button>
                                                </li>
                                                <li class="lnk wishlist"> <a class="add-to-cart" href="detail.html" title="Wishlist"> <i class="icon fa fa-heart"></i> </a> </li>
                                                <li class="lnk"> <a class="add-to-cart" href="detail.html" title="Compare"> <i class="fa fa-signal"></i> </a> </li>
                                            </ul>
                                        </div>
                                        <!-- /.action -->
                                    </div>
                                    <!-- /.cart -->
                                </div>
                                <!-- /.product-info -->
                            </div>
                            <!-- /.col -->
                        </div>
                        <!-- /.product-list-row -->
                        <div class="tag new"><span>new</span></div>
                    </div>
                    <!-- /.product-list -->
                </div>
                <!-- /.products -->
            </div>
            <!-- /.category-product-inner -->
            ';
        }
        listproducts ($result_cat_name['cat_id']);
    }
}
echo listproducts();
?>