gpt4 book ai didi

php - 如何根据 php 中的表值使菜单动态化?

转载 作者:行者123 更新时间:2023-11-29 05:52:55 26 4
gpt4 key购买 nike

下表将主菜单和子菜单放在同一列中。我根据menu_codesubmenu提到了menu_root_id来区分main_menusubmenu。我需要根据表格动态显示 mainmenu 下的所有 submenu 到以下 HTML 菜单代码中。请帮助我如何做到这一点?

enter image description here

这是我的 html 静态菜单代码:

         <!-- sidebar menu -->
<ul class="sidebar-menu" data-widget="tree">
<li class="nav-devider"></li>
<li class="header nav-small-cap"></li>

<li class="treeview">
<a href="#">
<i class="fa fa-database"></i>
<span>Main Menu1</span>
<span class="pull-right-container">
<i class="fa fa-angle-right pull-right"></i>
</span>
</a>
<ul class="treeview-menu">
<li><a href="">Sub menu1</a></li>
<li><a href="">Sub menu2</a></li>
<li><a href="">Sub menu3</a></li>
</ul>
</li>

<li class="treeview">
<a href="#">
<i class="fa fa-user"></i>
<span>Main Menu2</span>
<span class="pull-right-container">
<i class="fa fa-angle-right pull-right"></i>
</span>
</a>
<ul class="treeview-menu">
<li><a href="">Sub menu4</a></li>
<li><a href="">Sub menu5</a></li>
<li><a href="">Sub menu6</a></li>
</ul>
</li>
</ul>

这是我的动态代码:但显示不正确。

                          <!-- sidebar menu -->
<ul class="sidebar-menu" data-widget="tree">
<li class="nav-devider"></li>
<li class="header nav-small-cap"></li>
<?php
$query = $this->db->get('create_menu');
$this->db->order_by("menu_order_no", "asc");
if($query -> num_rows() > 0){
foreach ($query->result_array() as $row) {
?>
<li class="treeview">
<a href="#">
<i class="fa fa-database"></i>
<span><?php if($row['menu_root_id'] == "")
{ echo $row['menu_name']; }
?>
</span>
<span class="pull-right-container">
<i class="fa fa-angle-right pull-right"></i>
</span>
</a>

<ul class="treeview-menu">
<?php ?> <li><a href=""><?php print_r($row['menu_name']); ?></a></li><?php ?>
</ul>
</li>
<?php } } ?>
</ul>

最佳答案

试试这个:-

<ul class="sidebar-menu" data-widget="tree">
<li class="nav-devider"></li>
<li class="header nav-small-cap"></li>
<?php
// Get here all the main menu's in $query by having the condition that menu_root_id=null
if($query->num_rows() > 0) {
foreach ($query->result_array() as $row) {
?>
<li class="treeview">
<a href="#">
<i class="fa fa-database"></i>
<span><?php echo $row['menu_name']; ?></span>
<span class="pull-right-container">
<i class="fa fa-angle-right pull-right"></i>
</span>
</a>
<ul class="treeview-menu">
<?php
// Get here all the sub menu's in $query1 by having the condition that menu_root_id=$row['menu_code']
if($query1->num_rows() > 0) {
foreach ($query1->result_array() as $row1) {
?>
<li><a href="<?php echo $row1['menu_path']; ?>"><?php echo $row1['menu_name']; ?></a></li>
<?php
}
}
?>
</ul>
</li>
<?php
}
}
?>
</ul>

简单。基本思想是首先获取所有主菜单,然后在其上循环。之后遍历子菜单并获取带有链接的名称。
我不知道CI。所以我留下了查询区作为评论。

关于php - 如何根据 php 中的表值使菜单动态化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52531306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com