简体   繁体   中英

Dynamic multi level Menu and submenu php mysql

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

 <?php

$sql =('SELECT rubriques.id,rubriques.intitule,actions.intitulee,actions.lien,actions.idr   FROM rubriques,actions where rubriques.id=actions.idr ');
$stmt = $conn->query($sql);
    if($stmt->num_rows > 0)
    {

while($row=$stmt->fetch_assoc())
{
    extract($row);
    ?>

          <li class="active"><a href="index.html"><?php echo $intitule; ?></a>
          <ul class="dropdown">

                <li><a href="<?php echo $lien; ?>"><?php echo $intitulee; ?></a></li>
              </ul>



    <?php

   }  }         

  ?>  

for example (wht i want):

if A is menu item and A1 A2 A3 are sub menu item what i want is a menu like this A

A1

A2

A3

but what i get whith this code is

AAA

A1 A2 A3

 ```CREATE TABLE IF NOT EXISTS `actions` (
     `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
     `intitulee` varchar(255) NOT NULL,
     `lien` varchar(255) NOT NULL,
     `idr` int(255) NOT NULL,
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;



     INSERT INTO `actions` (`id`, `intitulee`, `lien`, `idr`) VALUES
     (1, 'Estivage', 'estirage.php', 1),
     (4, 'Excursions', 'exurcions.html', 1),
     (5, 'Equipe foot', '404.html', 2),
     (6, 'Clubs de sports ', '404.html', 0),
      (7, 'Fete des femmes', '404.html', 3),


 CREATE TABLE IF NOT EXISTS `rubriques` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`intitule` varchar(255) NOT NULL,
 PRIMARY KEY (`id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;



   INSERT INTO `rubriques` (`id`, `intitule`) VALUES
   (1, 'Voyages'),
   (2, 'ACTIVITES CULTURELLES ET SPORTIVES.'),
   (3, 'FETES & RECEPTIONS'),

It would be more usual to see that query written like this:

$sql = "
SELECT r.id
     , r.intitule
     , a.intitulee
     , a.lien
     , a.idr   
  FROM rubriques r
  JOIN actions a
    ON a.idr = r.id
 ORDER 
    BY r.id;
    ";

As your menu and sub-menu are in different table you can select menu first and then select sub-menu depending on the menu selected .ie :

 <?php
//getting menu first
$sql  = 'SELECT id,intitule FROM rubriques';
$stmt = $conn->query($sql);
if ($stmt->num_rows > 0) {
   while($row = $stmt->fetch_assoc()){
       //getting values  
        $intitule = $row['intitule '];
        $idr = $row['id']; ?>
       <!--your menu-->
    <li class="active"><a href="index.html"><?php
        echo $intitule;
     ?></a>
  <?php
   //passing the id from first to action table for compare and retrieve that rows only
        $sql1  = 'SELECT  * FROM actions where idr= ' . $idr;
        $stmt1 = $conn->query($sql1);
   ?>
     <ul class="dropdown">
    <?php
        while ($row1 = $stmt->fetch_assoc()) {
            $lien  = $row1['lien'];
            $intitulee = $row1['intitulee'];
    ?>

        <!--your submenu-->
  <li><a href="<?php echo $lien;?>"><?php echo $intitulee; ?></a></li>

     <?php

        }
    ?>
       </ul> <!--closing ul -->

     </li>
<?php
  }//closing while
} //closing if
?> 

the final code

  <?php
   //getting menu first
   $sql  = 'SELECT id,intitule FROM rubriques ';
   $stmt = $conn->query($sql);
   if ($stmt->num_rows > 0) {
   while ($row = $stmt->fetch_assoc()){
   //getting values  
    $intitule =$row['intitule'];
    $id = $row['id']; ?>
   <!--your menu-->
<li class="active"><a href="index.html"><?php
    echo $intitule;
 ?></a>

 <?php
   //passing the id from first to action table for compare and retrieve that rows only
       $sql1  = 'SELECT  * FROM actions where idr= ' . $id;
    $stmt1 = $conn->query($sql1);
       ?>
    <ul class="dropdown">
      <?php
        while ($row1 = $stmt1->fetch_assoc()) {
        $lien = $row1['lien'];
        $intitulee = $row1['intitulee'];
?>

    <!--your submenu-->
<li><a href="<?php echo $lien;?>"><?php echo $intitulee; ?></a></li>

 <?php

    } 
?>
   </ul> <!--closing ul -->
 </li>
   <?php
    }}
      $conn->close();
     //closing if
     ?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM