...

View Full Version : Recursive Count Function



josh.beck
06-30-2007, 06:23 AM
Hey All,

Nice to be here.

I have a recursive function that counts the number of members in a multi-level downline by querying a mysql db.

For some reason as the downline grows the function start multiplying the count at the final iteration. I can't explain why it's doing this or debug a solution.


function CountDownline ($member_id, $count = 0)
{
$result = $this->db->ExecuteSql ("Select member_id From `members` Where placement_id='$member_id' And is_cust=0 And member_id!='$member_id'");
while ($row = $this->db->FetchInArray ($result))
{
$mem_id = $row['member_id'];
$refCount = $this->db->GetOne ("Select Count(*) From `members` Where placement_id='$mem_id' And is_cust=0 And member_id!='$mem_id'", 0);
if ($refCount > 0)
{
$refCount = $this->CountDownline ($mem_id, $count);
$count += $refCount;
}
$count++;
}
$this->db->FreeSqlResult ($result);

return $count;
}

If someone could give it a look and tell me why this is happening I would appreciate it, we are pre-launching this biz and I need to get this resolved asap.

Thanks a bunch.

Josh

digital-ether
06-30-2007, 09:52 AM
I believe you should have:


function CountDownline ($member_id, $count = 0)
{
$result = $this->db->ExecuteSql ("Select member_id From `members` Where placement_id='$member_id' And is_cust=0 And member_id!='$member_id'");
while ($row = $this->db->FetchInArray ($result))
{
$mem_id = $row['member_id'];
$refCount = $this->db->GetOne ("Select Count(*) From `members` Where placement_id='$mem_id' And is_cust=0 And member_id!='$mem_id'", 0);
if ($refCount > 0)
{
$count += $refCount;
$this->CountDownline ($mem_id, $count);

}
$count++;
}
$this->db->FreeSqlResult ($result);

return $count;
}

I believe before you were adding the total $count each time you counted the "children" of one of the "members".

josh.beck
06-30-2007, 01:35 PM
Well that didn't quite get it but certainly got me closer.

I banged my head against the desk a few times and came up with this:


//----------------------------------------------------------------------------
function CountFrontline ($member_id, $count = 0)
{
$count = $this->db->GetOne ("Select Count(*) From members Where placement_id='$member_id' And is_cust='0' And member_id!='$member_id'", 0);
$count = ($count > 0) ? $count : 0;

return $count;
}

//----------------------------------------------------------------------------
function CountDownline ($member_id, $count = 0)
{
$count += $this->CountFrontline ($member_id);
$result = $this->db->ExecuteSql ("Select member_id From `members` Where placement_id='$member_id' And is_cust='0' And member_id!='$member_id'");
while ($row = $this->db->FetchInArray ($result))
{
$member = $row['member_id'];
$refs = $this->CountFrontline ($member);
if ($refs > 0)
{
$count += $refs;
$next = $this->CountDownline ($member, $count);
$count++;
}
}
$this->db->FreeSqlResult ($result);

return $count;
}

I added a new function to take care of counting each member's frontline and moved the counter inside the if statement and now she works - FINALLY!

Don't ask me to tell you why it works LOL - it just does, and now I can put down the coffee cup and go to sleep :)

Thanks for your help, I appreciate your time :)

Josh



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum