irkevin Posted July 29, 2009 Share Posted July 29, 2009 Hello everyone, I have a function that return some links from mysql DB. Below is the actual code function generate_menu($parent){ $has_childs = false; global $show_menu; foreach($show_menu as $key => $value){ if($value['parent_id'] == $parent){ if($has_childs === false){ $has_childs = true; } if($value['parent_id'] == '0'){ echo '<div id='.$value['class_id'].$value['class'].'>'; echo '<div id='.$value['class_id'].$value['style_id'].'>'.$value['title'].'</div>'; echo '<ul id='.$value['class_id'].$value['list_style'].'>'; }else{ echo '<li><a href="">'.$value['title'].'</a>'; } generate_menu($key); echo '</li>'; } } if($has_childs === true){ echo '</ul>'; echo '</div>'; } } As you can see. I'm echoing html inside the function. Is there a way i can return the values instead of echoing it straightly into the function? Quote Link to comment Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 just declare a variable at the beginning of the function... and replace all the echo's and concatenate the variable you set. then return the variable. function generate_menu($parent){ $output = ''; $output .= '<div id='.$value['class_id'].$value['class'].'>'; return $output; Quote Link to comment Share on other sites More sharing options...
irkevin Posted July 29, 2009 Author Share Posted July 29, 2009 Grrrrrrrrr im such a .......... !! Earlier i was doing $list = ''; $list .= etc etc return $list; But then when calling the function, i was just typing generate_menu(0)... it should have been echo generate_menu(0) Anyways, thanks.. Tcare Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.