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;
}
Bartosz Ciechanowski
10.3k5 gold badges48 silver badges61 bronze badges
asked Nov 22, 2012 at 18:09
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.

answered Nov 22, 2012 at 18:15
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.

answered Nov 22, 2012 at 18:15
1
  • thanks very much pal, you are absolutely right. it works perfectly as I expected it to be. Commented Nov 22, 2012 at 18:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.