嗨我正在尝试使用子菜单创建动态菜单。我差不多完成了,但循环重复,你可以在输出图像中看到,这不应该发生。错误就像1。水果主菜单正在重复。[2] .fruits子类别正在拆分和显示。
menu.php
$where_array = array('role_id' => $currentRolename , 'active_status' => '1'); //step1
$table = 'bg_assignment';
$query = $this->db->where($where_array)->get($table);
foreach ($query->result() as $perm)
{
$bg = $perm->bg_id;
$temp = '1';
$where_array2 = array('role_id' => $currentRolename , 'permission' => '1'); //step2
$table2 = 'role_permission';
$query2 = $this->db->where($where_array2)->get($table2);
foreach ($query2->result() as $r2)
{
$bg_id = $r2->bg_id;
$where_array3 = array('bg_id' => $bg_id);
$table3 = 'bg_forms';
$query3 = $this->db->where($where_array3)->get($table3);
foreach ($query3->result() as $r3)
{
$where_array4 = array('phpfile_name' => $r3->phpfile_name);
$table4 = 'bg_forms';
$query4 = $this->db->where($where_array4)->get($table4);
foreach ($query4->result() as $r4)
{
?>
<li class="treeview <?php echo menu_li_active($r4->category_name); ?>">
<a href="#">
<i class="fa fa-sitemap"></i>
<span> <?php echo ($r4->category_name); ?></span>
<i class="fa fa-angle-left pull-right"></i>
</a>
<ul class="treeview-menu">
<?php echo menu_link($r4->controller.'/'.$r4->phpfile_name, $r4->displayform_name); ?>
</ul>
<?php } }?>
</li>
</li>
<?php
}
}
?>
答案 0 :(得分:0)
我没有做任何测试,只是在这里提出一些想法,我认为如果你只是在查询中使用内连接会更容易和更清楚。下面的代码可能有语法错误,我没有和你一样的环境来进行测试。
<?php
//Let me just use open query to make this clear.
$sql_query = '
select rp.bg_id,
bf.category_name,
bf.controller,
bf.phpfile_name,
bf.displayform_name
from bg_assignment ba
inner join Role_permission rp
on ba.role_id = rp.role_id
and ba.bg_id = rp.bg_id --You might also want this line
inner join Bg_forms bf
on bg_id = bf.bg_id
where rp.permission = 1
and ba.active_status = 1
order by rp.bg_id, bf.displayform_name
';//I missed this semicolon
//Above sql query will give you two records
//Please test above query in your mysql to see if you can get below two records
/*
bg_id category_name controller phpfile_name displayform_name
5 Fruits Product index Apple
5 Frutis Product sales Orange
*/
$query = $this->db->query($sql_query);
$result = $query->result_array();
if(count($result) > 0){ // We get records!
$html = '';
for($i = 0; $i<count($result); $i++){
$s = $result[$i];
//Make the first li no matter what
if($i === 0){
$html.= '<li class="treeview">'.menu_li_active($s->category_name);
$html.= '<a href="#">';
$html.= '<i class="fa fa-sitemap"></i>';
$html.= '<span>'.$s->category_name.'</span>';
$html.= '<i class="fa fa-angle-left pull-right"></i>';
$html.= '</a>';
$html.= '</li>';
$html.= '<ul class="treeview-menu">';
$html.= menu_link($s->controller.'/'.$s->phpfile_name, $s->displayform_name);
if(count($result)===1){
$html.= '</ul>';
}
}
else{
if($s->category_name === $result[$i-1]->category_name){
//If current category is the same as previous one, we put them in one tab
$html.= menu_link($s->controller.'/'.$s->phpfile_name, $s->displayform_name);
}
else{
//If different, create a new tab
$html.='</ul>';
$html.= '<li class="treeview">'.menu_li_active($s->category_name);
$html.= '<a href="#">';
$html.= '<i class="fa fa-sitemap"></i>';
$html.= '<span>'.$s->category_name.'</span>';
$html.= '<i class="fa fa-angle-left pull-right"></i>';
$html.= '</a>';
$html.= '</li>';
$html.= '<ul class="treeview-menu">';
$html.= menu_link($s->controller.'/'.$s->phpfile_name, $s->displayform_name);
}
}
}
$html.='</li>';
echo $html;
}
?>