为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕田峪4524236 2021-11-13 18:56:15
我有一个需要从数据库动态创建的菜单。需要有菜单和子菜单 <?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 是子菜单项我想要的是这样的菜单 AA1A2A3但我得到的这段代码是AAAA1 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 回答

?
偶然的你

TA贡献1841条经验 获得超3个赞

由于您menu和sub-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

?> 


查看完整回答
反对 回复 2021-11-13
?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

最终代码


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

     ?> 


查看完整回答
反对 回复 2021-11-13
?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

看到这样写的查询会更常见:


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

    ";


查看完整回答
反对 回复 2021-11-13
  • 3 回答
  • 0 关注
  • 165 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号