簡體   English   中英

php mysql 動態多級菜單和子菜單

[英]Dynamic multi level Menu and submenu php mysql

我有一個需要從數據庫動態創建的菜單。 需要有菜單和子菜單

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

   }  }         

  ?>  

例如(我想要什么):

如果 A 是菜單項而 A1 A2 A3 是子菜單項我想要的是這樣的菜單 A

A1

A2

A3

但我得到的這段代碼是

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'),

看到這樣寫的查詢會更常見:

$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;
    ";

由於您的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
?> 

最終代碼

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