这是我的代码
<ul class="nav side-menu">
<?php
$mnquery=$ob->showDataall("select * from menus where parent_id='0' and delet='0'");
foreach($mnquery as $value)
{
$selquery=$ob->showDataall("select * from menu_permission where staff_id='".$_SESSION['user_id']."'");
foreach($selquery as $nquery)
{
foreach(explode(",",$nquery['permission']) as $mnu)
{
if($value['menu_id']==$mnu)
{
?>
<li><a <?php if($value['menu_hyper']!='#'){ echo 'href="'.$value['menu_hyper'].'"';}?>><i class="fa fa-bars"></i><?php echo $value['menu_name']; if($value['menu_hyper']=='#'){?><span class="fa fa-chevron-down"></span><?php }?></a>
<?php
$submnquery=$ob->showDataall("select * from menus where parent_id='".$mnu."' and delet='0'");
if(!empty($submnquery))
foreach($submnquery as $subvalue)
{
if($subvalue['parent_id']==$mnu)
{
?>
<ul class="nav child_menu" style="display: none">
<li><a <?php if($subvalue['menu_hyper']!='#'){ echo 'href="'.$subvalue['menu_hyper'].'"';}?>><?php echo $subvalue['menu_name']; if($subvalue['menu_hyper']=='#'){?><span class="fa fa-chevron-down"></span><?php }?></a>
<?php
$chldmnquery=$ob->showDataall("select * from menus where parent_id='".$subvalue['menu_id']."' and delet='0'");
if(!empty($chldmnquery))
foreach($chldmnquery as $chldvalue)
{
if($chldvalue['parent_id']==$subvalue['menu_id'])
{
?>
<ul class="nav child_menu" style="display: none">
<li><a href="<?php echo $chldvalue['menu_hyper'];?>"><?php echo $chldvalue['menu_name'];?></a></li>
</ul>
<?php
}
}
?>
</li>
</ul>
<?php
}
}
?>
</li>
<?php
}
}
}
}
?>
</ul>
这是我的函数showDataall
public function showDataall($result)
{
$q = $this->conn->prepare($result) or die("failed!");
$q->execute();
while ($r = $q->fetch(PDO::FETCH_ASSOC))
{
$data[] = $r;
}
return $data;
}
这是我的名为“菜单”
的表格 menu_id menu_name menu_hyper parent_id delet
1 Master Settings # 0 0
2 Add New Menu ../pages/menu_add.php 1 0
3 Menu Permission ../pages/menu_permissions.php 1 0
4 Role ../pages/add_role.php 1 0
5 Staff ../pages/add_staff.php 1 0
6 Add Product ../pages/product_add.php 0 0
这是我的另一个名为' menu_permission '
的表格 id staff_id permission
1 1 1,2,3,4,5,6
2 2 1,4
我允许' staff_id = 1 '的所有菜单权限 我允许menu_id 1 和 4 代表' staff_id = 2 ' 显示的所有菜单中都记录了 staff_id = 1 ,但所有菜单中都记录了 staff_id = 2 。我只允许staff_id = 2的两个菜单(menu_id = 1和menu_id = 2)如何解决这个问题
请帮助我....
答案 0 :(得分:0)
尝试更改此部分:
foreach(explode(",",$nquery['permission']) as $mnu)
到:
$mypermission = explode(",",$nquery['permission']);
然后
foreach($mypermission as $mnu)
答案 1 :(得分:0)
我写了我的知识...它的工作完全正常任何人帮我简化这段代码
<?php
$mnquery=$ob->showDataall("select * from menus where parent_id='0' and delet='0'");
foreach($mnquery as $value)
{
$selquery=$ob->showDataall("select * from menu_permission where staff_id='".$_SESSION['user_id']."'");
foreach($selquery as $nquery)
{
foreach(explode(",",$nquery['permission']) as $mnu)
{
if($value['menu_id']==$mnu)
{
?>
<li><a <?php if($value['menu_hyper']!='#'){ echo 'href="'.$value['menu_hyper'].'"';}?>><i class="fa fa-bars"></i><?php echo $value['menu_name']; if($value['menu_hyper']=='#'){?><span class="fa fa-chevron-down"></span><?php }?></a>
<?php
$submnquery=$ob->showDataall("select * from menus where parent_id='".$mnu."' and parent_id!='0' and delet='0'");
if(!empty($submnquery))
foreach($submnquery as $subvalue)
{
$selquery=$ob->showDataall("select * from menu_permission where staff_id='".$_SESSION['user_id']."'");
foreach($selquery as $nquery)
{
foreach(explode(",",$nquery['permission']) as $mnua)
{
if($subvalue['menu_id']==$mnua)
{
?>
<ul class="nav child_menu" style="display: none">
<li><a <?php if($subvalue['menu_hyper']!='#'){ echo 'href="'.$subvalue['menu_hyper'].'"';}?>><?php echo $subvalue['menu_name']; if($subvalue['menu_hyper']=='#'){?><span class="fa fa-chevron-down"></span><?php }?></a>
<?php
$chldmnquery=$ob->showDataall("select * from menus where parent_id='".$subvalue['menu_id']."' and delet='0'");
if(!empty($chldmnquery))
foreach($chldmnquery as $chldvalue)
{
$selquery=$ob->showDataall("select * from menu_permission where staff_id='".$_SESSION['user_id']."'");
foreach($selquery as $nquery)
{
foreach(explode(",",$nquery['permission']) as $mnub)
{
if($chldvalue['menu_id']==$mnub)
{
?>
<ul class="nav child_menu" style="display: none">
<li><a href="<?php echo $chldvalue['menu_hyper'];?>"><?php echo $chldvalue['menu_name'];?></a></li>
</ul>
<?php
}
}
}
}
?>
</li>
</ul>
<?php
}
}
}
}
?>
</li>
<?php
}
}
}
}
?>
</ul>