我可以使用以下代码从php数组创建菜单。但是,如何在我的数据库中存储行,然后从中以及随后的菜单层次结构中创建数组? 如果某些jquery插件可以帮助我,我很乐意添加它们,以便我可以构建前端菜单(例如拖放),然后应使用以下命令创建它:
菜单阵列:
$primary_nav = array(
array(
'name' => 'Dashboard',
'url' => 'index.php',
'icon' => 'fa fa-dashboard',
),
array(
'name' => 'General Settings',
'icon' => 'fa fa-cog',
'sub' => array(
array(
'name' => 'Profile Settings',
'url' => 'profilesettings.php',
),
array(
'name' => 'Account Settings',
'url' => 'accountsettings.php'
)
)
),
array(
'name' => 'Users Manager',
'icon' => 'fa fa-users',
'sub' => array(
array(
'name' => 'Users Management',
'url' => 'users_management.php',
),
array(
'name' => 'Groups & Permissions',
'url' => 'groups_permissions.php',
),
)
),
array(
'name' => 'Message Center',
'url' => 'message_center.php',
'icon' => 'fa fa-inbox',
),
array(
'name' => 'Email Center',
'url' => 'email_center.php',
'icon' => 'fa fa-envelope',
),
array(
'name' => 'User Interface',
'icon' => 'fa fa-rocket',
'sub' => array(
array(
'name' => 'Widgets',
'url' => 'page_ui_widgets.php',
),
array(
'name' => 'Elements',
'sub' => array(
array(
'name' => 'Blocks & Grid',
'url' => 'page_ui_blocks_grid.php'
),
array(
'name' => 'Typography',
'url' => 'page_ui_typography.php'
),
array(
'name' => 'Buttons & Dropdowns',
'url' => 'page_ui_buttons_dropdowns.php'
),
array(
'name' => 'Navigation & More',
'url' => 'page_ui_navigation_more.php'
),
array(
'name' => 'Progress & Loading',
'url' => 'page_ui_progress_loading.php'
),
array(
'name' => 'Tables',
'url' => 'page_ui_tables.php'
)
)
),
array(
'name' => 'Forms',
'sub' => array(
array(
'name' => 'Components',
'url' => 'page_forms_components.php'
),
array(
'name' => 'Wizard',
'url' => 'page_forms_wizard.php'
),
array(
'name' => 'Validation',
'url' => 'page_forms_validation.php'
)
)
),
array(
'name' => 'Icon Packs',
'sub' => array(
array(
'name' => 'Font Awesome',
'url' => 'page_ui_icons_fontawesome.php'
),
array(
'name' => 'Glyphicons Pro',
'url' => 'page_ui_icons_glyphicons_pro.php'
)
)
)
)
),
);
使用以下PHP,我可以创建菜单:
<ul class="sidebar-nav">
<?php foreach( $primary_nav as $key => $link ) {
$link_class = '';
$li_active = '';
$menu_link = '';
// Get 1st level link's vital info
$url = (isset($link['url']) && $link['url']) ? $link['url'] : '#';
$active = (isset($link['url']) && ($template['active_page'] == $link['url'])) ? ' active' : '';
$icon = (isset($link['icon']) && $link['icon']) ? '<i class="' . $link['icon'] . ' sidebar-nav-icon"></i>' : '';
// Check if the link has a submenu
if (isset($link['sub']) && $link['sub']) {
// Since it has a submenu, we need to check if we have to add the class active
// to its parent li element (only if a 2nd or 3rd level link is active)
foreach ($link['sub'] as $sub_link) {
if (in_array($template['active_page'], $sub_link)) {
$li_active = ' class="active"';
break;
}
// 3rd level links
if (isset($sub_link['sub']) && $sub_link['sub']) {
foreach ($sub_link['sub'] as $sub2_link) {
if (in_array($template['active_page'], $sub2_link)) {
$li_active = ' class="active"';
break;
}
}
}
}
$menu_link = 'sidebar-nav-menu';
}
// Create the class attribute for our link
if ($menu_link || $active) {
$link_class = ' class="'. $menu_link . $active .'"';
}
?>
<?php if ($url == 'separator') { // if it is a separator and not a link ?>
<li class="sidebar-separator">
<i class="fa fa-ellipsis-h"></i>
</li>
<?php } else { // If it is a link ?>
<li<?php echo $li_active; ?>>
<a href="<?php echo $url; ?>"<?php echo $link_class; ?>><?php if (isset($link['sub']) && $link['sub']) { // if the link has a submenu ?><i class="fa fa-chevron-left sidebar-nav-indicator sidebar-nav-mini-hide"></i><?php } echo $icon; ?><span class="sidebar-nav-mini-hide"><?php echo $link['name']; ?></span></a>
<?php if (isset($link['sub']) && $link['sub']) { // if the link has a submenu ?>
<ul>
<?php foreach ($link['sub'] as $sub_link) {
$link_class = '';
$li_active = '';
$submenu_link = '';
// Get 2nd level link's vital info
$url = (isset($sub_link['url']) && $sub_link['url']) ? $sub_link['url'] : '#';
$active = (isset($sub_link['url']) && ($template['active_page'] == $sub_link['url'])) ? ' active' : '';
// Check if the link has a submenu
if (isset($sub_link['sub']) && $sub_link['sub']) {
// Since it has a submenu, we need to check if we have to add the class active
// to its parent li element (only if a 3rd level link is active)
foreach ($sub_link['sub'] as $sub2_link) {
if (in_array($template['active_page'], $sub2_link)) {
$li_active = ' class="active"';
break;
}
}
$submenu_link = 'sidebar-nav-submenu';
}
if ($submenu_link || $active) {
$link_class = ' class="'. $submenu_link . $active .'"';
}
?>
<li<?php echo $li_active; ?>>
<a href="<?php echo $url; ?>"<?php echo $link_class; ?>><?php if (isset($sub_link['sub']) && $sub_link['sub']) { ?><i class="fa fa-chevron-left sidebar-nav-indicator"></i><?php } echo $sub_link['name']; ?></a>
<?php if (isset($sub_link['sub']) && $sub_link['sub']) { ?>
<ul>
<?php foreach ($sub_link['sub'] as $sub2_link) {
// Get 3rd level link's vital info
$url = (isset($sub2_link['url']) && $sub2_link['url']) ? $sub2_link['url'] : '#';
$active = (isset($sub2_link['url']) && ($template['active_page'] == $sub2_link['url'])) ? ' class="active"' : '';
?>
<li>
<a href="<?php echo $url; ?>"<?php echo $active ?>><?php echo $sub2_link['name']; ?></a>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
<?php } ?>
</ul>