2016-08-12 40 views
0

我有一個表作爲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是在有1帖子ID和2的話,它會顯示板球,職位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> 

回答

0

要獲得數在每個類別的職位,你需要使用下面的SQL查詢:

SELECT 
    blog_category.*, 
    COUNT(blog_posts.postID) AS cnt 
FROM 
    blog_category 
INNER JOIN 
    blog_posts 
ON 
    blog_posts.categoryID=blog_category.categoryID 
GROUP BY 
    blog_category.categoryID; 

因此,這將是這樣的:

<ul> 
<?php 
    if (mysqli_connect_errno()) { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
    //here we use a modified query 
    $result = mysqli_query($conn, "SELECT 
    blog_category.*, 
    COUNT(blog_posts.postID) AS cnt 
FROM 
    blog_category 
INNER JOIN 
    blog_posts 
ON 
    blog_posts.categoryID=blog_category.categoryID 
GROUP BY 
    blog_category.categoryID;"); 
     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"><?php echo $row['cnt'];?></span> 
     </li> 
     <?php } ?> 
    </ul> 
+0

工作正常,謝謝:* –

0

宥可以direcly與查詢獲取數據

的完整列表

select blog_category.catName group_concat(blog_posts.postID) 
from blog_posts 
inner join blog_category on blog_category.CategoryID = blog_posts.CategoryID 
group by blog_category.catName ; 

或singole類別

select blog_category.catName group_concat(blog_posts.postID) 
from blog_posts 
inner join blog_category on blog_category.CategoryID = blog_posts.CategoryID 
where blog_category.CategoryID = 1 
group by blog_category.catName ; 
1

你必須改變你的SQL查詢來獲取數據。有很多方法可以加入它。我寫了一個。

SQL

SELECT blog_category.* , count(*) as total_post 
FROM blog_category INNER JOIN blog_posts 
ON blog_category.CategoryID = blog_posts.categoryID 
GROUP BY blog_category.Category 

全碼

<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"><?php echo $row['total_post']; ?></span> 
    </li><?php 

    } 

?></ul> 

欲瞭解更多信息

SQL連接:http://www.w3schools.com/sql/sql_join_inner.asp
SQL計數:http://www.w3schools.com/sql/sql_func_count.asp