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

时间:2019-08-05 14:21:37

标签: php html 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

但是我得到的这段代码是

A A A

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

3 个答案:

答案 0 :(得分:0)

不是答案;评论太久了:

(至少从1992年开始),看到这样写的查询会更常见:

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

答案 1 :(得分:0)

由于您的menusub-menu在不同的表中,因此您可以先选择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
?> 

答案 2 :(得分:0)

最终代码

could not import tkg/services/articles/api/article (no parsed files for package tkg/services/articles/api/article)
相关问题