【发布时间】:2016-08-12 18:10:03
【问题描述】:
我有一张表作为 blog_posts
+---------+-----------+
| postID | categoryID |
+========+============+
| 1 | 1 |
+--------+------------+
| 2 | 1 |
+--------+------------+
| 3 | 2 |
+--------+------------+
| 4 | 4 |
+--------+------------+
另一个是 blog_category
+---------+-----------+
| CategoryID | catName |
+========+============+
| 1 | cricket |
+--------+------------+
| 2 | sports |
+--------+------------+
| 3 | football |
+--------+------------+
| 4 | tennis |
+--------+------------+
现在我想显示类别以及类别中有多少帖子。就像 categoryID 1 在 postID 1 和 2 中一样,它会显示 cricket ,帖子 2。现在我只获取类别,这很容易:P
<ul>
<?php
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($conn,"SELECT * FROM blog_category ");
while($row = mysqli_fetch_array($result)) {
?>
<li>
<a class="f-categories-filter_name" href="blog-cat.php?id=<?php echo $row['categoryID'];?>"><i class="fa fa-plus"></i><?php echo $row['categoryName'];?></a>
<span class="b-categories-filter_count f-categories-filter_count">**I want to display here number of posts of that category**</span>
</li>
<?php } ?>
</ul>
【问题讨论】: