【问题标题】:Dynamic Drop Down Menu and submenu php mysqli with slug value带有 slug 值的动态下拉菜单和子菜单 php mysqli
【发布时间】:2020-06-11 02:53:05
【问题描述】:

我有一个需要从数据库动态创建的菜单。需要有菜单和子菜单

例如(我想要的是):

<li class="<?= ($pg == 'departments') ? 'active':''; ?>">
   <a href="departments">Departments</a>
   <ul class="dropdown">
      <li>
         <a href="department">CSE</a><br>
         <a href="department">ECE</a>
         <ul class="dropdown dropdown-right">
           
            <li ><a href="department?slug=about-cse-department">About The Department</a></li>
            <li ><a href="head-of-department?slug=about-cse-hod">HOD</a></li>
            <li ><a href="department-vision-mission?slug=about-cse-vision-mision">Vision &amp; Mission</a></li>
            <li ><a href="department-facilities?slug=about-cse-department-facility">Department Facilities</a></li>
            <li ><a href="department-goals?slug=about-cse-department-goals">Department Goals</a></li>
            <?php } } ?>
         </ul>
      </li>
      <?php } ?>
   </ul>
</li>

主要部门和子菜单是 CSE、ECE、EEE、CIVIL,我在下面的下拉菜单中会出现每个子菜单,在 mysqli 表中我还将创建和存储数据和 slug 名称。并且下拉菜单也会重复,请在下面找到附件。

我的答案是

部门-> CSE->关于部门 部门-> ECE->关于部门

部门-> CSE->HOD 部门-> ECE->HOD

每一个只有一个菜单。

我的代码是

    <li class="<?= ($pg == 'departments') ? 'active':''; ?>">
   <a href="departments">Departments</a>
   <ul class="dropdown">
      <?php $sql2 ="SELECT  * from  `departments` ";
         $result2 = $conn->query($sql2);
           while($row2 = $result2->fetch_assoc())
             {
               $department = $row2['dept_name']; ?>
      <li>
         <a href="department"><?=$department;?></a><!-- Department Names as shoen image-->
         <ul class="dropdown dropdown-right">
            <?php  $sql3 ="SELECT  * from  `page` WHERE page_department = '$department' ";
               $result3 = $conn->query($sql3);
               while($row3 = $result3->fetch_assoc())
               {
                  $pname = $row3['page_name'];
                    echo $slug = $row3['page_slug']; ?>
            <li ><a href="department?slug=<?=$slug;?>">About The Department</a></li>
            <li ><a href="head-of-department?slug=<?=$slug;?>">HOD</a></li>
            <li ><a href="department-vision-mission?slug=<?=$slug;?>">Vision &amp; Mission</a></li>
            <li ><a href="department-facilities?slug=<?=$slug;?>">Department Facilities</a></li>
            <li ><a href="department-goals?slug=<?=$slug;?>">Department Goals</a></li>
            <li ><a href="department-faculty?slug=<?=$slug;?>">Faculty</a></li>
            <li ><a href="department-publications?slug=<?=$slug;?>">Faculty Publications</a></li>
            <li ><a href="department-syllabus?slug=<?=$slug;?>">Syllabus</a></li>
            <li ><a href="department-workshops-seminars?slug=<?=$slug;?>">Workshops</a></li>
            <li ><a href="department-student-toppers?slug=<?=$slug;?>">Student Toppers</a></li>
            <?php } } ?>
         </ul>
      </li>
      <?php } ?>
   </ul>
</li>

在上面运行的代码中,我可以上传这种类型的图片。

我的部门表和页表如下所示

【问题讨论】:

  • 显示你的代码,你尝试了什么
  • 菜单和子菜单是否来自数据库
  • 是的菜单和子菜单在数据库中
  • 展示你的完整代码并给出表格
  • 我在检查@Tausif 后更新我的代码

标签: javascript php jquery drop-down-menu submenu


【解决方案1】:
you are repeating complete menu in while loop so its showing multiple time, as you already fetching sub menu by table then just create a single link and fetch related sub menu name and url with database but as i found you have different hrefs on your sub menu so you placed multiple links in submenu here you need to update your page table , you need to create a column as code below ,  hope this work:

    <li class="<?= ($pg == 'departments') ? 'active':''; ?>">
    <a href="departments">Departments</a>
    <ul class="dropdown">
      <?php $sql2 ="SELECT  * from  `departments` ";
         $result2 = $conn->query($sql2);
           while($row2 = $result2->fetch_assoc())
             {
               $department = $row2['nit_dept_name']; ?>
      <li>
         <a href="department"><?=$department;?></a><!-- Department Names as shoen image-->
         <ul class="dropdown dropdown-right">
            <?php  $sql3 ="SELECT  * from  `page` WHERE page_department = '$department' ";
               $result3 = $conn->query($sql3);
               while($row3 = $result3->fetch_assoc())
               {
                  $pname = $row3['page_name'];
                  $slug = $row3['page_slug']; 
                  $href= $row3['href']; //create a column in page table for href 


             ?>
            <li ><a href="<?php echo $href;?>?slug=<?=$slug;?>"><?php echo $pname;?></a></li>

            <?php }  ?>
         </ul>
       </li>
      <?php } ?>
     </ul>
    </li>

【讨论】:

  • 此代码是否从数据库中创建动态分层类别?
【解决方案2】:

你重复的while循环一次检查这个

<ul class="dropdown">
  <?php $sql2 ="SELECT  * from  `departments` ";
     $result2 = $conn->query($sql2);
       while($row2 = $result2->fetch_assoc())
         {
           $department = $row2['nit_dept_name']; ?>
  <li>
     <a href="department"><?=$department;?></a><!-- Department Names as shoen image-->
     <ul class="dropdown dropdown-right">
        <?php  $sql3 ="SELECT  * from  `page` WHERE page_department = '$department' ";
           $result3 = $conn->query($sql3);
           while($row3 = $result3->fetch_assoc())
           {
              $pname = $row3['page_name'];
              $slug = $row3['page_slug']; 
              $href= $row3['href']; //create a column in page table for href 


         ?>
        <li ><a href="<?php echo $href;?>?slug=<?=$slug;?>"><?php echo $pname;?></a></li>

        <?php }  ?>
     </ul>
   </li>
  <?php } ?>
 </ul>

【讨论】:

    猜你喜欢
    • 2014-12-04
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 2016-06-18
    • 2015-07-14
    • 1970-01-01
    相关资源
    最近更新 更多