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
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;

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.