Search code examples
javascriptphpjquerydrop-down-menusubmenu

Dynamic Drop Down Menu and submenu php mysqli with slug value


I have a menu that needs to be created dynamically from the database. need to have menu and submenu

for example (what i want is ):

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

main Departments and the submenu is CSE, ECE, EEE, CIVIL and each submenu i have below dropdown menu will came and in mysqli table also i will create and store data and slug names.And also drowndown menu will repeated please find below attachment.

My answer is

Department-> CSE->About The Department Department-> ECE->About The Department

Department-> CSE->HOD Department-> ECE->HOD

with each one only single menu. in this image you can see dropdown menus are repeated i have not repeated each menu will different slug

My Code is

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

in the above code running like image can i upload this type will came.

my tables of department and page tables look like this department and page

Page table


Solution

  • your repeating while loop once check this

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