我正在接受`
注意:未定义的索引:index.php in 第26行的xxxxx / index.php
当我从menu.php中删除$ submenu变量数组时。一切正常,但当我添加$ submenu数组变量时,我收到错误消息。 ` 我在这里要做的是首先获得最高级别,然后搜索他们的子任务是否为子项目
在index.php文件中我有这段代码
foreach($menu as $menu){
$title = $menu[0];
$privllages = $menu[1];
$template = $menu[2];
$class = $menu[3];
$id = $menu[4];
$icon = $menu[5];
if(!$title){$title = 'No Title';}
if($class){$class = 'class="'.$class.'"';} else {$class = '';}
if($id){$id = 'id="'.$id.'"';} else {$id = '';}
if(!$icon){$icon = "menu-default.png";}
echo '<a href="'.$template.'" '.$class.' '.$id.'>' . $title . '</a>' . "<br/>";
/* LINE 26 Starts here */
if($submenu[$template]){
foreach($submenu[$template] as $submenu){
echo '<a href="#">' . $submenu[0] . '</a>' . "<br/>";
}
}
}
在menu.php文件
上$menu[1] = array('Dashboard', '0', 'index.php', 'sidebar-menu','','menu-dashboard');
$menu[2] = array('Pages', '0', 'pages.php', 'sidebar-menu','','menu-dashboard');
$menu[3] = array('Media', '0', 'media.php', 'sidebar-menu','','menu-dashboard');
$menu[4] = array('Tools', '0', 'tools.php', 'sidebar-menu','','menu-dashboard');
$menu[5] = array('Users', '0', 'users.php', 'sidebar-menu','','menu-dashboard');
$menu[6] = array('Settings', '0', 'settings.php', 'sidebar-menu','','menu-dashboard');
$submenu['pages.php'][1] = array( 'All Links 1', 'admin-menu', 'manage_links', 'link-manager.php' );
$submenu['pages.php'][2] = array( 'All Links 2', 'admin-menu', 'manage_links', 'link-manager.php' );
$submenu['pages.php'][3] = array( 'All Links 3', 'admin-menu', 'manage_links', 'link-manager.php' );
$submenu['pages.php'][4] = array( 'All Links 4', 'admin-menu', 'manage_links', 'link-manager.php' );
答案 0 :(得分:4)
而不是
if($submenu[$template]){
使用
if(!empty($submenu[$template])){
这将仅在$ submenu [$ template]存在且不为空时触发
答案 1 :(得分:1)
尝试使用此修改后的代码。代码中提到了错误
<?php
$menu[1] = array('Dashboard', '0', 'index.php', 'sidebar-menu','','menu-dashboard');
$menu[2] = array('Pages', '0', 'pages.php', 'sidebar-menu','','menu-dashboard');
$menu[3] = array('Media', '0', 'media.php', 'sidebar-menu','','menu-dashboard');
$menu[4] = array('Tools', '0', 'tools.php', 'sidebar-menu','','menu-dashboard');
$menu[5] = array('Users', '0', 'users.php', 'sidebar-menu','','menu-dashboard');
$menu[6] = array('Settings', '0', 'settings.php', 'sidebar-menu','','menu-dashboard');
$submenu['pages.php'][1] = array( 'All Links 1', 'admin-menu', 'manage_links', 'link-manager.php' );
$submenu['pages.php'][2] = array( 'All Links 2', 'admin-menu', 'manage_links', 'link-manager.php' );
$submenu['pages.php'][3] = array( 'All Links 3', 'admin-menu', 'manage_links', 'link-manager.php' );
$submenu['pages.php'][4] = array( 'All Links 4', 'admin-menu', 'manage_links', 'link-manager.php' );
foreach($menu as $arr_menu){ // problem-1 was here foreach($menu as $menu)
$title = $arr_menu[0];
$privllages = $arr_menu[1];
$template = $arr_menu[2];
$class = $arr_menu[3];
$id = $arr_menu[4];
$icon = $arr_menu[5];
if(!$title){$title = 'No Title';}
if($class){$class = 'class="'.$class.'"';} else {$class = '';}
if($id){$id = 'id="'.$id.'"';} else {$id = '';}
if(!$icon){$icon = "menu-default.png";}
echo '<a href="'.$template.'" '.$class.' '.$id.'>' . $title . '</a>' . "<br/>";
if(isset($submenu[$template])){ // problem-2 was here not checking isset
foreach($submenu[$template] as $arr_submenu){ // problem-3 was here foreach($submenu as $submenu)
echo '<a href="#">' . $arr_submenu[0] . '</a>' . "<br/>";
}
}
}
?>
答案 2 :(得分:1)
更改
if($submenu[$template])
到
if(!empty($submenu[$template]))
以下行也不明确
foreach($menu as $menu)
将其更改为
foreach($menu as $my_menu)