Jump to content

Count of categories and subcategories PHP count


nepoverljiv
 Share

Recommended Posts

Anyone can help me?

 

<?php

$tbl_name="menu";

$kategorije = mysql_query("SELECT * FROM $tbl_name WHERE Left(menu_name, 1) BETWEEN 'A' AND 'M' ORDER BY menu_name ASC");

if (!$kategorije) {

die("Database query failed: " . mysql_error());

}

 

while ($row=mysql_fetch_array( $kategorije )) {

echo "<div id=lista-header><h4>{$row["menu_name"]}</h4></div>";

$id_sub=$row['id_menu'];

$podkategorije = mysql_query("SELECT * FROM submenu WHERE id_menu=$id_sub ORDER BY sub_name ASC", $connection);

if (!$podkategorije) {

die("Database query failed: " . mysql_error());

}

echo "<ul class=\"pod\">";

while ($pod=mysql_fetch_array( $podkategorije )) {

echo "<li><a href=index.php?=podkate?".$pod["id_sub"]." class=black>{$pod["sub_name"]}</a><hr size=1 align=left width=100px color=#cccccc></li>";

}

echo "</ul>";

}

 

 

?>

 

In this way, I get list with categories and hes subcategories. How to count how many subcategories have each categories, and count how many articles have each categories?

 

Example (I wanna get this kind of look):

 

Categories name (3)

subcategoriesname (2)

subcategoriesname (4)

subcategoriesname (7)

 

Categories name (5)

subcategoriesname (1)

subcategoriesname (14)

subcategoriesname (9)

subcategoriesname (2)

subcategoriesname (8)

 

 

Categories name (2)

subcategoriesname (28)

subcategoriesname (17)

 

Where the numbers represent how many categories and sub-items have articles

Link to comment
Share on other sites

You could nest query iterations as such:

 

$sql = 'SELECT cat_id, cat_name FROM main_categories';
$result1 = mysql_query($sql);
while ($row1 = mysql_fetch_assoc($result1)){
   $sql = "SELECT sub_cat_id, cat_name FROM sub_categories WHERE main_cat_id={$row1['cat_id']}";
   $result2 = mysql_query($sql);
   echo $row1['cat_name'].' ('.mysql_num_rows($result2).')';
   while ($row2 = mysql_fetch_assoc($result2)){
       $sql = "SELECT COUNT(topic) Topic_Count FROM cat_topics WHERE sub_cat_id={$row2['sub_cat_id']}";
       $result3 = mysql_query($sql);
       $row3 = mysql_fetch_assoc($result3);
       echo $row2['cat_name'].' ('.$row3['Topic_Count'].')';
   }
}

 

That's a general solution.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.