0

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

  • show your code , what you tried – Tausif Feb 27 '20 at 06:34
  • does menu and submenu are from database – Tausif Feb 27 '20 at 06:34
  • yes menu and submenus are in database – Nagendra Kumar Feb 27 '20 at 06:35
  • show your complete code and give tables – Tausif Feb 27 '20 at 06:38
  • i update my code once check @Tausif – Nagendra Kumar Feb 27 '20 at 06:39
  • *I have a menu that needs to be created dynamically from the database. need to have menu and submenu*, Is this your answer ? https://stackoverflow.com/questions/59334057/how-to-select-subcategories-from-selected-category-using-a-nested-function-in-php yes it’s :) –  Feb 27 '20 at 06:59
  • here my problem is main category and sub category retrive well but sub sub category will retrive same as all sub categories but only change in the slug value with out repeat sub sub menu is my intention @Dlk. and above link is another perspective – Nagendra Kumar Feb 27 '20 at 07:02
  • Ah sorry I thought this *Department-> CSE->About The Department*, means hierarchical categories, nested! My bad :) your question is not clear then, needs more clarification. –  Feb 27 '20 at 07:08
  • **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Feb 27 '20 at 23:02

2 Answers2

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>
Tausif
  • 420
  • 2
  • 15
  • Does this code creates dynamic hierarchical categories from database ? –  Feb 27 '20 at 07:03
1

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>