Click to See Complete Forum and Search --> : Heirarchical <ul> Menu


atw
06-10-2007, 04:30 PM
Here is my existing code: function display_children() {
$nav_menu = "<ul>\n";
$parent_result = mysql_query( "SELECT id, name FROM developments WHERE parent_id IS NULL AND visible = 'y' ORDER BY 'name' ASC;" );
while( $parent_row = mysql_fetch_assoc( $parent_result )) {
$parent_id = $parent_row['id'];

$nav_menu .= "\t<li>" . $parent_row['name'] . "\n";

$child_result = mysql_query( "SELECT id, name FROM developments WHERE parent_id = '$parent_id' AND visible = 'y' ORDER BY 'name' ASC;" );
while( $child_row = mysql_fetch_assoc( $child_result )) {

$nav_menu .= "\t\t<li>" . $child_row['name'] . "</li>\n";

}
$nav_menu .= "\t</li>\n";
}
$nav_menu .= "</ul>\n";
return $nav_menu;
}
This code generates this output:<ul>
<li>item1
<li>subitem1-1</li>
</li>
<li>item2
<li>subitem2-1</li>
<li>subitem2-2</li>
</li>
</ul>
As you can see it does work but I need to get the script to add opening and closing <ul>s around the subitems (and their respective <li>s).

Any help would be gratefully received...

NogDog
06-10-2007, 05:10 PM
function display_children() {
$nav_menu = "<ul>\n";
$parent_result = mysql_query( "SELECT id, name FROM developments WHERE parent_id IS NULL AND visible = 'y' ORDER BY 'name' ASC;" );
while( $parent_row = mysql_fetch_assoc( $parent_result )) {
$parent_id = $parent_row['id'];

$nav_menu .= "\t<li>" . $parent_row['name'] . "\n";

$child_result = mysql_query( "SELECT id, name FROM developments WHERE parent_id = '$parent_id' AND visible = 'y' ORDER BY 'name' ASC;" );
if(mysql_num_rows($child_result)) // <-------
{
echo "<ul>\n"; // <-------
while( $child_row = mysql_fetch_assoc( $child_result )) {

$nav_menu .= "\t\t<li>" . $child_row['name'] . "</li>\n";

}
echo "</ul>\n"; // <-------
}
$nav_menu .= "\t</li>\n";
}
$nav_menu .= "</ul>\n";
return $nav_menu;
}

atw
06-10-2007, 06:31 PM
You are the man!

I just had to make a small tweak (the echo()s should have been "$nav_menu .="s).

Thank you so much for the help! :)