我想构建一个数据库驱动的多级菜单,我从[http://abhijitpal.in/][1]得到它,但问题是我不能只将class = "dropdown"
放到顶级菜单<ul>
,它也适用于子菜单。我刚修改了一点,但我不知道如何使用递归函数。以下是代码,请查看是否可以提供帮助
<?php /** Function to display Catelogue Menu */
//select all rows from the main_menu table
$q ="SELECT * FROM catelogue WHERE cat_visibility = '1'";
$r = mysqli_query($dbc, $q);
//create a multidimensional array to hold a list of menu and parent menu
$menu = array(
'menus' => array(),
'parent_menus' => array()
);
//build the array lists with data from the menu table
while ($row = mysqli_fetch_assoc($r)) {
//creates entry into menus array with current menu id ie. $menus['menus'][1]
$menu['menus'][$row['cat_id']] = $row;
//creates entry into parent_menus array. parent_menus array contains a list of all menus with children
$menu['parent_menus'][$row['cat_parentid']][] = $row['cat_id'];
}
// Create the main function to build milti-level menu. It is a recursive function.
function nav_catelogue($parent, $menu) {
//$html = "";
if (isset($menu['parent_menus'][$parent])) { ?>
<ul>
<?php
foreach ($menu['parent_menus'][$parent] as $menu_id) {
if (!isset($menu['parent_menus'][$menu_id])) { ?>
<li><a href="<?php echo $menu['menus'][$menu_id]['link']; ?>"><?php echo $menu['menus'][$menu_id]['cat_name']; ?></a></li>
<?php }
if (isset($menu['parent_menus'][$menu_id])) { ?>
<li><a href="#"><?php echo $menu['menus'][$menu_id]['cat_name']; ?></a>
<?php echo nav_catelogue($menu_id, $menu); ?>
</li>
<?php }
} ?>
</ul>
<?php }
}
?>
我的数据库结构
-----------------------------------------
| cat_id | cat_name | cat_parentid |
-----------------------------------------
| 1 | Home | 0 |
| 2 | About | 0 |
| 3 | Contact | 0 |
| 4 | History | 2 |
| 5 | Services | 2 |
-----------------------------------------
我想要的输出:
这是@Mave
的最终代码<?php /** Function to display Catelogue Menu */
//select all rows from the main_menu table
$q ="SELECT * FROM catelogue WHERE cat_visibility = '1'";
$r = mysqli_query($dbc, $q);
//create a multidimensional array to hold a list of menu and parent menu
$menu = array(
'menus' => array(),
'parent_menus' => array()
);
//build the array lists with data from the menu table
while ($row = mysqli_fetch_assoc($r)) {
//creates entry into menus array with current menu id ie. $menus['menus'][1]
$menu['menus'][$row['cat_id']] = $row;
//creates entry into parent_menus array. parent_menus array contains a list of all menus with children
$menu['parent_menus'][$row['cat_parentid']][] = $row['cat_id'];
}
// Create the main function to build milti-level menu. It is a recursive function.
function nav_catelogue($parent, $menu, $top = false) {
if (isset($menu['parent_menus'][$parent])) {
//this is short code for if($top === true) { //do true } else { //do false }
echo $top ? '<ul class="dropdown">' : '<ul>';
foreach ($menu['parent_menus'][$parent] as $menu_id) {
if (!isset($menu['parent_menus'][$menu_id])) {
echo '<li><a href="' . $menu['menus'][$menu_id]['link'] . '">' . $menu['menus'][$menu_id]['cat_name'] . '</a></li>';
}
if (isset($menu['parent_menus'][$menu_id])) {
echo '<li><a href="#">' . $menu['menus'][$menu_id]['cat_name'] . '</a>' . nav_catelogue($menu_id, $menu) . '</li>';
}
}
echo '</ul>';
}
}
?>
答案 0 :(得分:1)
function nav_catelogue($parent, $menu, $top = false) {
if (isset($menu['parent_menus'][$parent])) {
//this is short code for if($top === true) { //do true } else { //do false }
echo $top ? '<ul class="dropdown">' : '<ul>';
foreach ($menu['parent_menus'][$parent] as $menu_id) {
if (!isset($menu['parent_menus'][$menu_id])) {
echo '<li><a href="' . $menu['menus'][$menu_id]['link'] . '">' . $menu['menus'][$menu_id]['cat_name'] . '</a></li>';
}
if (isset($menu['parent_menus'][$menu_id])) {
echo '<li><a href="#">' . $menu['menus'][$menu_id]['cat_name'] . '</a>' . nav_catelogue($menu_id, $menu) . '</li>';
}
}
echo '</ul>';
}
}
当您第一次调用nav_catelogue(当前代码中没有)时,请使用nav_catelogue($menu_id, $menu, true);
答案 1 :(得分:0)
你有两种方法可以使你的递归函数放在
class="dropdown"
第一种方法是调用call / callee样式,其中你的调用函数将处理第一种情况(class =&#34; dropdown&#34;)然后调用递归函数来处理一般情况(没有班级=&#34;下拉&#34;)。
添加一个你在第一次调用时传输的参数(布尔值?),它将添加class =&#34; dropdown&#34;。在递归调用中,您将此布尔值传输为false(无类=&#34;下拉&#34;插入)。