PDA

View Full Version : Problem with foreach


akanevsky
05-28-2005, 05:47 PM
I am making a function which is supposed to get the total of all items within a category and its subcategories. Here it is.

function get_count($id)
{
global $categorycache;

$data = $DB_site->query_first("SELECT count(*) as cnt FROM thetable WHERE cat_id='$id'");
$count += $data['cnt'];

if (is_array($categorycache["$id"])) // in categorycache, it is all sorted by the parent. So $id here server as a parent as well.
{
foreach ($categorycache["$id"] as $category)
{
$count += get_count($category['cat_id']);
}
}

return $count;
}

THe problem is, it does not count the numbers in the right way. For every category, it shows the same number of items. In the root, it shows far more items for every category, then there is total in the database. Is there any way to fix this?

EDIT:

Nevermind, my bad. I #*$&(*^# ran the function with the same static var for every category ::MAD::