1

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;
}
1
  • It doesn't look like the value of $this->itemId is being changed at all Commented Nov 22, 2012 at 18:14

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

thanks very much pal, you are absolutely right. it works perfectly as I expected it to be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.