Jump to content

[SOLVED] funtion and return


irkevin

Recommended Posts

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

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;

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.