user1845827
user1845827

Reputation: 319

Recursive function issue in PHP

I put the below function inside a class

I try to call it again inside this function (I have made a recursive call inside this function $this->buildMenu($this->itemId, $menuData);)

only without the recursive call this function works, else it does not return any value.

// menu builder function, parent_id 0 is the root 
function buildMenu($parent_id, $menuData)
{       
    $this->html = ''; 
    if (isset($menuData['parents'][$parent_id])) 
    { 
        if($parent_id=='0') $this->html = '<ul class="menu responsive-menu sf-js-enabled">';
        else $this->html = '<ul>';
        foreach ($menuData['parents'][$parent_id] as $this->itemId) 
        { 
            if($this->itemId=='1'){$this->html .= '<li><a href="'.PATH.'">' . $menuData['items'][$this->itemId]['menu_name'].'</a>';}
            else $this->html .= '<li class=""><a href="?action='. md5($menuData['items'][$this->itemId]['menu_link']).'">' . $menuData['items'][$this->itemId]['menu_name'].'</a>';
            $this->html .= $this->buildMenu($this->itemId, $menuData); 
            $this->html .= '</li>'; 
        } 
        $this->html .= '</ul>'; 
    } 

    return $this->html;
}

Upvotes: 1

Views: 218

Answers (2)

EyalAr
EyalAr

Reputation: 3170

The problem might be with $this->html = '';.
It seems like $html is a class variable, so each recursive call to the function, you initialize it to be an empty string.
Try using it as a local function variable.

Upvotes: 1

Keith Gaughan
Keith Gaughan

Reputation: 22675

First off, replace $this->html with $html: you're throttling the value of it with an empty string when your recurse, which isn't what you're intending, I'm guessing.

Upvotes: 0

Related Questions