Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
Register FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 05-28-2005, 04:47 PM
akanevsky akanevsky is offline
 
Join Date: Apr 2005
Posts: 3,972
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Problem with foreach

I am making a function which is supposed to get the total of all items within a category and its subcategories. Here it is.

PHP Code:
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::
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:18 PM.


Powered by vBulletin® Version 3.9.0
Copyright ©2000 - 2017, vBulletin Solutions Inc.