PHP数据库驱动的多级菜单 [英] PHP Database driven Multilevel Menu
问题描述
我想构建一个数据库驱动的多级菜单,我从[ http://abhijitpal.in/]中获得了它[1] ,但是这里的问题是我不能只将class = "dropdown"
放在顶部菜单<ul>
中,它也同样适用于子菜单.我只是做了一点修改,但是我不知道如何使用递归函数.下面是代码,请查看是否可以提供帮助
I want to built a database driven multilevel menu I got it from [http://abhijitpal.in/][1] but the problem here is i can't put class = "dropdown"
only to the top menus <ul>
, it also get applies to the sub menus as well. I just modified a little but I don't know how to use recursive function. Below is the code, please see if you can help
<?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 |
-----------------------------------------
我想要的期望输出:
- 首页
- 关于
- 历史
- 服务
- HOME
- ABOUT
- History
- Services
这是@Mave的最终代码
This is the final code as per @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>'; } } ?>
推荐答案
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);
When you first call nav_catelogue (not present in your current code), call it with
nav_catelogue($menu_id, $menu, true);
这篇关于PHP数据库驱动的多级菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!