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? Link to comment https://forums.phpfreaks.com/topic/168033-solved-funtion-and-return/ 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; Link to comment https://forums.phpfreaks.com/topic/168033-solved-funtion-and-return/#findComment-886253 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 Link to comment https://forums.phpfreaks.com/topic/168033-solved-funtion-and-return/#findComment-886270 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.