Vejo muitas dúvidas a respeito, e várias soluções são apresentadas, como executando uma query várias vezes, ou limitando o nível de profundidade dos filhos. Segue um idéia que tive, usando apenas uma query e montando o array para posteriormente construir o menu. E aproveitando para embelezar a visualização, utilizei o projeto de TreeView do site http://jquery.bassistance.de/treeview/!
<base href="http://jquery.bassistance.de/treeview/">
<script src="../dist/jquery.js" type="text/javascript"></script>
<script src="jquery.treeview.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#browser").Treeview();
});
</script>
<style type="text/css">
.treeview, .treeview ul {
padding: 0;
margin: 0;
list-style: none;
}
ul.
dir li
{ padding: 2px
0 0 16px;
}
.treeview li { background: url(images/tv-item.gif) 0 0 no-repeat; }
.treeview .collapsable { background-image: url(images/tv-collapsable.gif); }
.treeview .expandable { background-image: url(images/tv-expandable.gif); }
.treeview .last { background-image: url(images/tv-item-last.gif); }
.treeview .lastCollapsable { background-image: url(images/tv-collapsable-last.gif); }
.treeview .lastExpandable { background-image: url(images/tv-expandable-last.gif); }
</style>
<?php
// Filhos
function show_tree($id_parent) {
foreach ($menu[$id_parent] as $id => $label) {
printf("<li><img src='images/folder.gif'/> %s\n",
$label);
show_tree($id);
} else {
printf("<li><img src='images/file.gif'/> %s\n",
$label);
}
}
}
$rs =
mysql_query('SELECT * FROM menu ORDER BY id_parent, label');
$menu['root'][$row['id']] = $row['label'];
} else {
$menu[$row['id_parent']][$row['id']] = $row['label'];
}
}
print "<ul id='browser' class='dir'>\n";
// Raiz
foreach ($menu['root'] as $id => $label) {
printf("<li><img src='images/folder.gif'/>%s\n",
$label);
show_tree($id);
}
}
?>