Jump to content

Reqursive function outputs multi-level array


fallenboy

Recommended Posts

Hi guys. I have a problem and i cant solve it b myself. Im pretty sure it has an easy solution but I just can't figure it out... :(

 

This code outputs nested unordered list, but I need it to output array, multi level:

function do_menu($parent, $menu) {

$result = do_mysql("SELECT id, title FROM pages WHERE parent='{$parent}' ORDER BY id");

if(mysql_affected_rows() != 0) {

	$menu .= "<ul>\n";

	while ($row = mysql_fetch_array($result)) {

		$menu .= "<li><a href=\"".ADMINURL."menu/index.php?section=open&id=".$row['id']."\">".stripslashes(htmlspecialchars_decode($row['title']))."</a>";

		$menu = do_menu($row['id'], $menu);
		$menu .= "</li>\n";            
	}
	$menu .= "</ul>\n";

} else $menu = false;

return $menu;
}

 

Can somebody help me, please?

 

Thanks guys ;)

 

PS! If this function sucks, it's OK as I'm not very good programmer, more HTML-guy :)

Link to comment
Share on other sites

can you tell us what problem you're having with the function you posted? I can see a few things like not doing $menu .= on the recursive call, but there may be deeper issues I'm not seeing.

 

The function works fine, but it outputs HTML. I want it to output multilevel array:

 

now.

<ul>
<li>menu 1
<ul>
<li>menu 1 1</li>
<li>menu 1 2</li>
</ul>
</li>
<ul>

but i want it to output multievel PHP array.

array ->menu[1] -> array(id => 2, parent =>1), array(id =>3, parent => 1) 

 

I just cant make it work :(

 

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.