FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

counting entries for main category, then subcat

 


spider
below is the code I have for main categories and subcategories to try and get the count of items in the main category and in its subcategories..
add them together, so whatever the total is for the subcategories, it adds to whatever the number is for the main category.

right now, for what I have,

1 item in each category and 3 items all together in subcategories... the main category is suppose to show 4 items, and when click that main category it shows a list with one item, and 2 subcategories have a count of items in both of them...

what am i doing wrong, and is there a way to use less queries for this, I thought of JOIN, but it messes up my count for numrows...

Code:

$query_main_cat = "SELECT * FROM class_categories WHERE sub='0' ORDER BY cat";
$result_main_cat = mysql_query($query_main_cat);
$numrows_main_cat = mysql_num_rows($result_main_cat);

while($main_cat = mysql_fetch_array($result_main_cat)){
$main_cat_id = $main_cat['id'];
$main_cat_sub = $main_cat['sub'];
$main_cat_cat = $main_cat['cat'];

$query_sub_cat = "SELECT * FROM class_categories WHERE sub='$main_cat_id' ORDER BY cat";
$result_sub_cat = mysql_query($query_sub_cat);
$numrows_sub_cat = mysql_num_rows($result_sub_cat);

$query_main_items = "SELECT * FROM class_item WHERE cat_id='$main_cat_id'";
$result_main_items = mysql_query($query_main_items) or die ("Query_Items failed");
$numofrows_main_items = mysql_num_rows($result_main_items);
while($sub_cat = mysql_fetch_array($result_sub_cat)) {

$sub_cat_id = $sub_cat['id'];
$sub_cat_sub = $sub_cat['sub'];
$sub_cat_cat = $sub_cat['cat'];

$query_sub_items = "SELECT * FROM class_item WHERE cat_id='$sub_cat_id'";
$result_sub_items = mysql_query($query_sub_items) or die ("Query_Items failed");
$numofrows_sub_items = mysql_num_rows($result_sub_items);
}

$n = $numofrows_sub_items+$numofrows_main_items;

echo '<font size="2">( '.$n.' ) ';
echo '<font size="2"><b> <a href="http://'.$domain.'/c/';
echo 'results.php?id='.$main_cat_id.'&sub='.$main_cat_sub.'">'.$main_cat_cat.'</a></b><BR>';


}
?>


been trying to figure this out for about 2 weeks...
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.