我试图用来自数据库的行做一个菜单,我一直在 stackoverflow 中阅读关于做一个递归函数来评估 parentId 和 Id 并构建较低级别的选项,但没有运气。还尝试将 sql 答案转换为多维数组,但没有运气...
基本上我需要的是根据父级构建列表元素集,例如 Dashboard.php 是 son.php 的父级
这是我的代码:
function buildMenu($userType){
try{
$db = new db();
$conn = $db->conn();
$SQL_BUILD_MENU = "select usertypes.id as menuId,
menu.name as menuName, menu.link as menuLink,
usertype_menu.parent_id as
parent from usertypes inner join usertype_menu on usertypes.id = usertype_menu.usertype_id inner join
menu on usertype_menu.menu_id =
menu.id where usertypes.name='".$userType."'";
$conn->prepare($SQL_BUILD_MENU);
foreach($check = $conn->query($SQL_BUILD_MENU) as $row) {
echo "<ul>";
echo "<a href=".$row['menuLink']."><li>" . $row['menuName'] . "</li></a>";
echo "</ul>";
}
}
catch(Exception $e){
echo "Se ha presentado un error en buildMenu".$e;
}
}
}
+----+--------+-------------+-----------------+--------+
| id | menuId | menuName | menuLink | parent |
+----+--------+-------------+-----------------+--------+
| 1 | 1 | Dashboard | dashboard.php | 0 |
| 2 | 1 | Interaction | interaction.php | 0 |
| 3 | 1 | Son | Son.php | 1 |
+----+--------+-------------+-----------------+--------+
提前致谢。