我正在制作一个菜单系统,我想拥有无限的孩子。
我从数据库中读取并显示父母下的第二个孩子,但这只允许我有一个孩子。如果没有剩下的孩子,我该如何重复?
我可以复制上面的代码,但我想继续向数据库添加条目,并在需要时继续创建更多子代。
$get_top_menu = mysqli_query($con,"SELECT * FROM ss_folderstructure WHERE folderstructure_parent='0'");
while($found_top_menu = mysqli_fetch_array($get_top_menu))
{
echo "<li class='dropdown' ><a href=''><span class='icon-folder-open'></span>" . $found_top_menu['folderstructure_name'] . "</a>";
echo "<ul>";
$get_mid_menu = mysqli_query($con,"SELECT * FROM ss_folderstructure WHERE folderstructure_parent='" . $found_top_menu['folderstructure_idno'] . "'");
while($found_mid_menu = mysqli_fetch_array($get_mid_menu))
{
echo "<li><a href='bootstrap.html'>" . $found_mid_menu['folderstructure_name'] . "</a></li>";
}
echo "</ul>";
echo "</li>";
}
答案 0 :(得分:1)
首先,您可以使用一个查询从数据库中获取所有数据。并从结果中形成数组。
像:
$result = array();
$get_top_menu = mysqli_query($con,"SELECT * FROM ss_folderstructure");
while($found_top_menu = mysqli_fetch_array($get_top_menu)) {
$result[$found_top_menu['folderstructure_parent']][] = $found_top_menu;
}
在此之后你应该使用递归:
function doLoop($foo,$currId) {
echo "<ul>";
foreach ($foo[$currId] as $bar) {
if (isset($bar['folderstructure_idno'])) {
echo "<li>";
doLoop($foo,$bar['folderstructure_idno']);
echo "</li>";
} else {
echo "<li><a>{$bar['folderstructure_name']}</a></li>";
}
}
echo "</ul>";
return true;
}
doLoop($result,0);