number of items in a category, recursive

From: Date: Tue, 29 Apr 2003 18:20:37 +0000
Subject: number of items in a category, recursive
Groups: php.general 
Request: Send a blank email to php-general+get-145680@lists.php.net to get a copy of this message
hi, i have a table setup like this: - categoryID - parentID (if the category is a sub-category) - name and then, a products table: - productID - categoryID - name - etc. what I need to do is check how many products are in a given category, and all sub-categories within it. I wrote the following function, but for whatever reason, the page just doesn't load when I try to call it. please tell me what's wrong with it: function numItems($cID,$total=0) { global $_DB, $db; // get info about this category $category = $db->getEntry("categories",$cID); // add to the total $getTotal = mysql_query("SELECT COUNT(*) AS totalITems FROM products WHERE categoryID = '$cID'"); $total += mysql_result($getTotal,0,totalItems); if (!($category["parentID"])) { $sub = mysql_query("SELECT COUNT(*) AS numSub FROM categories WHERE parentID = '$cID'",$_DB); $numSub = mysql_result($sub,0,numSub); if ($numSub>0) { // category has sub-categories, check them as well $total += $this->numItems($category["parentID"],$total); } } return $total; }

« previous php.general (#145680) next »