Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Recursive Count Function

    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.

    PHP Code:
    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
    Last edited by josh.beck; 06-30-2007 at 05:23 AM. Reason: spelling

  • #2
    New Coder
    Join Date
    May 2006
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I believe you should have:

    PHP Code:
    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".
    Fiji Web Design - where i do Joomla Web Design
    Bucabay.com - My blog

  • #3
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    //----------------------------------------------------------------------------
        
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •