0

<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>

4

1 回答 1

1

制作您的RetrieveAllSubMenu()函数,使其仅通过其 id 返回给定菜单的子菜单,而不是所有菜单

function RetrieveAllSubMenu($menu_id) {
  // your code
  $stmt = $dbh - > prepare("SELECT * FROM submenulist_tbl WHERE parent_id = :menuid");
      $stmt - > bindValue(':menuid', $menu_id, PDO::PARAM_STR);
 // more code
}

只为您提供其背后的逻辑,但这将仅返回一个基于 pass 的菜单的子菜单$menu_id

因此,当尝试获取子菜单时,您foreach应该看起来类似于以下内容:

foreach (RetrieveAllSubMenu($value['menuid']) as $value){

我还建议您将返回的结果命名为$value更好的名称,例如:

foreach (RetrieveAllSubMenu($value['menuid']) as $submenu){

因为您还用于菜单和子菜单,如果您不完全理解 $value 何时被内部语句$value覆盖,这可能会导致问题foreach

编辑

您的 foreach 语句应类似于:

foreach (RetrieveAllMenu() as $menu){
    echo'<li>';
    echo '<a href="'.$menu['menuurl'].'" id=""'.$menu['menuname'].'"">'.$menu['menuname'].'</a>';
    echo'<ul>';
    foreach (RetrieveAllSubMenu($menu['menuid']) as $submenu){
        echo'<li><a href="'.$submenu['submenuurl'].'" id=""'.$submenu['submenuname'].'"">'.$submenu['submenuname'].'</a></li>';
    }
}
于 2014-10-25T10:45:14.360 回答