I am trying to build an un-oredered list menu tree from my database in PHP and MySQL.
I have an array of page objects I am returning from the db. Each page object
Instead of querying the database recursively, you can just pull out all entries and make the output function recursive. It's often as trivial as:
function print_list($array, $parent=0) {
print "";
foreach ($array as $row) {
if ($row->parent_id == $parent) {
print "- $row->title";
print_list($array, $row->id); # recurse
print "
";
} }
print "
";
}
It's only important to nest s into . Or just use HTML and leave out the closing .
Actually this prints too many s, so I would check for existence of sublevels and avoid printing it directly.