简体   繁体   English

php mysql 动态多级菜单和子菜单

[英]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如果 A 是菜单项而 A1 A2 A3 是子菜单项我想要的是这样的菜单 A

A1 A1

A2 A2

A3 A3

but what i get whith this code is但我得到的这段代码是

AAA AAA

A1 A2 A3 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 :由于您的menusub-menu在不同的表格中,您可以先选择menu ,然后根据所选sub-menu选择sub-menu 。即:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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