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 :)

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 :(

 

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.