gpt4 book ai didi

php - 数据库驱动的永无止境的菜单系统

转载 作者:行者123 更新时间:2023-11-29 12:17:00 25 4
gpt4 key购买 nike

我正在开发一个菜单系统,我希望它有无限的子项。

我让它从数据库中读取并显示父级下的第二个子级,但这只允许我有 1 个子级。我怎样才能让它重复,直到没有更多的 child 留下?

我可以复制上面的代码,但我想继续向数据库添加条目,并在需要时继续创建更多子项。

$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>";
}

最佳答案

首先,您可以使用一个查询从数据库中获取所有数据。并根据结果形成数组。

喜欢:

$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);

关于php - 数据库驱动的永无止境的菜单系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29645807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com