<ul>
<li>Main Menu 1
<ul>
<li>Sub Menu 1.1
</li>
<li>Sub Menu 1.2
</li>
</ul>
</li>
<li>Main Menu 2
<ul>
<li>Sub Menu 2.1
</li>
<li>Sub Menu 2.2
</li>
</ul>
</li>
</ul>
以上是我正在尝试制作的无序列表的表示。我可以使用 foreach 制作这种列表,一个用于获取主菜单的功能和一个用于获取子菜单的功能。我想就如何制定某种条件来确定哪个子菜单用于哪个菜单征求建议,因为到目前为止,所有子菜单都同时显示在所有菜单中。应该有一个特定子菜单的特定菜单这是我工作的代码
服务器端
function RetrieveAllMenu() {
global $dbh;
$stmt = $dbh - > prepare("SELECT * FROM userlist_tbl WHERE username = ?");
$stmt - > bindValue(1, $_SESSION['login_user']);
$stmt - > execute();
$selected_row = $stmt - > fetch(PDO::FETCH_ASSOC);
$mem_id = $selected_row['user_id'];
if ($stmt - > execute()) {
if ($stmt - > rowCount() > 0) {
$stmt = $dbh - > prepare("SELECT * FROM rolemapping_tbl WHERE user_id = ?");
$stmt - > bindValue(1, $mem_id);
$stmt - > execute();
$selected_row = $stmt - > fetch(PDO::FETCH_ASSOC);
$rolelist_id = $selected_row['rolelist_id'];
if ($stmt - > execute()) {
$stmt = $dbh - > prepare("SELECT * FROM roledetails_tbl WHERE rolelist_id = ?");
$stmt - > bindValue(1, $rolelist_id);
$stmt - > execute();
$selected_row = $stmt - > fetch(PDO::FETCH_ASSOC);
if ($stmt - > execute()) {
if ($stmt - > rowCount() > 0) {
$menu_id = array();
while ($selected_row = $stmt - > fetch(PDO::FETCH_ASSOC)) {
$menu_id[] = array('menuid' => $selected_row['menulist_id'], );
}
$stmt = $dbh - > prepare("SELECT * FROM menulist_tbl WHERE menulist_id = :menuid");
$menu_name = array();
foreach($menu_id as $row) {
$stmt - > execute(array(':menuid' => $row['menuid']));
//while ($selected_row =$stmt->fetch(PDO::FETCH_COLUMN, 0)){
while ($selected_row = $stmt - > fetch(PDO::FETCH_ASSOC)) {
$menu_name[] = array('menuname' => $selected_row['menu_name'], 'menuurl' => $selected_row['menu_url'], 'menuflag' => $selected_row['menu_flag'], 'menuid' => $selected_row['menulist_id']);
}
}
return $menu_name;
}
}
}
}
}
}
function RetrieveAllSubMenu() {
global $dbh;
$menu_name = RetrieveAllMenu();
$stmt = $dbh - > prepare("SELECT * FROM submenulist_tbl WHERE parent_id = :menuid");
$submenu_name = array();
foreach($menu_name as $row) {
//$stmt->execute(array(':menuid' => $row['menuid']));
$stmt - > bindValue(':menuid', $row['menuid'], PDO::PARAM_STR);
$stmt - > execute();
while ($selected_row = $stmt - > fetch(PDO::FETCH_ASSOC)) {
$submenu_name[] = array('submenuname' => $selected_row['submenulist_name'], 'submenuurl' => $selected_row['submenulist_url'], 'submenuflag' => $selected_row['submenulist_flag']);
}
}
//print_r($submenu_name);
return $submenu_name;
//return in_array($menuid, $submenu_name);
}
这是html方面
<?php
echo'<ul>';
foreach (RetrieveAllMenu() as $value){
//echo'<input type="submit" value="'.$value['menuname'].'" name="'.$value['menuname'].'"/>';
echo'<li>';
echo '<a href="'.$value['menuurl'].'" id=""'.$value['menuname'].'"">'.$value['menuname'].'</a>';
echo'<ul>';
foreach (RetrieveAllSubMenu() as $value){
//if( $value['parentid'] === $value['menuid'] ){
echo'<li><a href="'.$value['submenuurl'].'" id=""'.$value['submenuname'].'"">'.$value['submenuname'].'</a></li>';
//}
}
echo'</ul>';
echo'</li>';
}
echo'</ul>';
?>
这就是我的代码的外观,它应该看起来像上面的代码。
<ul>
<li>Main Menu 1
<ul>
<li>Sub Menu 1.1
</li>
<li>Sub Menu 1.2
</li>
<li>Sub Menu 2.1
</li>
<li>Sub Menu 2.2
</li>
</ul>
</li>
<li>Main Menu 2
<ul>
<li>Sub Menu 1.1
</li>
<li>Sub Menu 1.2
</li>
<li>Sub Menu 2.1
</li>
<li>Sub Menu 2.2
</li>
</ul>
</li>
</ul>