0

I am working on a tree structure relationship - backwards. Meaning, I have users in the database who are referred to the site by other users and I need to retrieve upper referrers up to 10 levels high.

I am using anonymous function inside of my function for this but it says "Maximum function nesting level of '100' reached, aborting!" What am I doing wrong here?

Here is my code:

public function showSponsors($user_id, $count = 10)
{
    //The array to store referrers
    $Referrers = array();       
    //The anonymous function that uses Laravel Eloquent model to retrieve data
    $GetReferrers   = function($id) use(&$Referrers, &$count, &$GetReferrers)
    {           
        //Get the first referrer and store id as index and name as value            
        $Referrers[$upper = User::find($id)->referrer] = User::find($upper)->Name;

        //Make sure you do not return more than $count
        //Make sure the referrer returned has a referrer to return
        if(count($Referrers) < $count || !empty($upper || $upper != $user_id)){
            $count--; return $GetReferrers($upper);

        }
        //Otherwise return the referrers;
        return $Referrers;
    };

    //Run the function using the user id
    $GetReferrers($user_id);

    //return the results stored in the Referrers array      
    return $Referrers;
Hill
  • 419
  • 1
  • 7
  • 12
  • http://stackoverflow.com/questions/4293775/increasing-nesting-functions-calls-limit – Ashalynd Jun 07 '14 at 23:34
  • Problem is that it should only run up to 10 levels so why is ti reaching past 100? Where am I supposed to decrement it? I tried in the code as edited above... No go! The issue is not to increase the limit to the maximum level reached as someone suggested above... – Hill Jun 07 '14 at 23:47
  • I guess you should call your $GetReferrers as $GetReferrers($upper, $count) then. It does not have the access to the original $count variable within the nested call. – Ashalynd Jun 07 '14 at 23:58
  • Look at that Ashland! It worked! After decrementing $count with $count-- and passing the $count variable into the anonymous function which is used as a reference... It worked like a charm! – Hill Jun 08 '14 at 00:11

1 Answers1

0

I guess you should call your $GetReferrers as $GetReferrers($upper, $count) then. It does not have the access to the original $count variable within the nested call.

Ashalynd
  • 12,363
  • 2
  • 34
  • 37