我有一个类似下面的菜单表(请参阅 HTML 部分):
我想将数据转换为循环嵌套菜单。我希望它像:
这会将其输出为 HTML:
<ul>
<?php echo $renderProductMenuItemLv1; ?>
</ul>
和相关的PHP代码:
if($result = $mysqli->query("SELECT * FROM myTable WHERE type='Business_Line_Product_Level_1' AND parent='37' ORDER BY order_id ASC"))
{
while($row = $result->fetch_array())
{
$productLv1Title = $row["title"];
$productLv1Body = $row["body"];
$productLv1OriId = $row["id"];
if($result2 = $mysqli->query("SELECT * FROM page WHERE type='Business_Line_Product_Level_2' AND parent='$productLv1OriId' ORDER BY order_id ASC"))
{
while($row = $result2->fetch_array())
{
$productLv2Title = $row["title"];
$productLv2Body = $row["body"];
$productLv2OriId = $row["id"];
$renderProductMenuItemLv2 .= '
<li>
<a href="businessLineProductLv2.php?p='. $productLv2Id .'">
'. $productLv2Title .'
</a>
</li>
';
} // eof while row
} // eof if result
$renderProductMenuItemLv1 .= '
<li>
<a href="businessLineProductLv1.php?p='. $productLv1Id .'">
'. $productLv1Title .'
</a>
<ul>
'. $renderProductMenuItemLv2 .'
</ul>
</li>
';
} // eof while row
} // eof if result
$result->free_result();
但结果是我在每个菜单项上都获得了额外的子菜单项。
如何修复我的代码?