如何打印类别和子类别限制3仅设置子类别

时间:2019-03-11 13:55:52

标签: php

<div class="row">
    <?php $category = mysqli_query($conn,"SELECT * from category  ORDER BY title ASC");while($row = mysqli_fetch_array($category)) {?>
    <div class="category-box" style="width:18%;margin-right:2%; float:left; height:300px;position: relative;">
        <?php $image=$row['image'];if(empty($image)){$image="defualt.jpeg"; }?>
        <img src="myhomeportal/vendors/category-img/<?php echo $image;?>" width="100%; height:150px;">
        <h4 class="category-title"><?php echo mb_strimwidth ($row['title'], 0, 15, "...");?></h4>
        <a href="show-subcategories.php?cat_id=<?php echo $row['cat_id'];?>" class="cat_id_view"><button class="btn btn-primary">View More</button></a>
        <?php $subcat=$conn->query("SELECT * FROM sub_category WHERE (issue here how to set limit 3) cat_id=".$row['cat_id'] );while($row=$subcat->fetch_array()){ ?>
            <a href="show-childcategories.php"><h5 class="subcategory-title1"><?php echo mb_strimwidth ($row['title'], 0, 15, "...");?></h5></a>
        <?php }?>
    </div>      
<?php }?></div>

1 个答案:

答案 0 :(得分:0)

根据您的评论,您不能在LIMIT子句前使用WHERE

SELECT * FROM sub_category WHERE limit 3 cat_id=".$row['cat_id'] 

这应该是:

SELECT * FROM sub_category WHERE cat_id=".$row['cat_id']." LIMIT 3";

格式必须为:

SELECT
FROM
WHERE
LIMIT

侧面说明:但是,这将返回ist 3条记录,而不是最新的3条记录。

Useful link