I like @mario's solution, and have improved on it with the prevention of the excess <ul>
. I would just recommend doing an ORDER BY
on your SQL query to get the menu in the order you want (might even recommend a weight/sequence column be added to the schema.
Data setup:
$menu = array( // Presumed to have been coming from a SQL SELECT, populated for demo.
array('id'=>1,'title'=>'Menu 1', 'parent_id'=>null),
array('id'=>2,'title'=>'Sub 1.1', 'parent_id'=>1),
array('id'=>3,'title'=>'Sub 1.2', 'parent_id'=>1),
array('id'=>4,'title'=>'Sub 1.3', 'parent_id'=>1),
array('id'=>5,'title'=>'Menu 2', 'parent_id'=>null),
array('id'=>6,'title'=>'Sub 2.1', 'parent_id'=>5),
array('id'=>7,'title'=>'Sub Sub 2.1.1', 'parent_id'=>6),
array('id'=>8,'title'=>'Sub 2.2', 'parent_id'=>5),
array('id'=>9,'title'=>'Menu 3', 'parent_id'=>null),
);
Handling:
function has_children($rows,$id) {
foreach ($rows as $row) {
if ($row['parent_id'] == $id)
return true;
}
return false;
}
function build_menu($rows,$parent=0)
{
$result = "<ul>";
foreach ($rows as $row)
{
if ($row['parent_id'] == $parent){
$result.= "<li>{$row['title']}";
if (has_children($rows,$row['id']))
$result.= build_menu($rows,$row['id']);
$result.= "</li>";
}
}
$result.= "</ul>";
return $result;
}
echo build_menu($menu);
Output:
<ul>
<li>Menu 1<ul>
<li>Sub 1.1</li>
<li>Sub 1.2</li>
<li>Sub 1.3</li>
</ul></li>
<li>Menu 2<ul>
<li>Sub 2.1<ul>
<li>Sub Sub 2.1.1</li>
</ul></li>
<li>Sub 2.2</li>
</ul></li>
<li>Menu 3</li>
</ul>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…