表结构及测试数据如下:
在common.php共用函数里面加入:
//将数组转化为树形数组
function arrToTree($data,$pid){
$tree = array();
foreach($data as $k => $v){
if($v['pid'] == $pid){
$v['pid'] = arrToTree($data,$v['id']);
$tree[] = $v;
}
}
return $tree;
}
//左边菜单栏输出
function outMenu($group,$tree){
$html = '';
foreach($tree as $t){
if($t['group_id']==$group){
if(empty($t['pid'])){
$html .= '<li><a href="'.__APP__.'/'.$t['name'].'/index/" target="navTab" rel="'.$t['name'].'">'.$t['title'].'</a></li>';
}else{
$html .='<li><a>'.$t['title'].'</a><ul>';
$html .=outMenu($group,$t['pid']);
$html = $html.'</ul></li>';
}
}
}
return $html;
}
indexAction.class.php:
public function index() {
if (isset ( $_SESSION [C ( 'USER_AUTH_KEY' )] )) {
//显示菜单项
$menu = array ();
//读取数据库模块列表生成菜单项
$node = M ( "Node" );
$where ['status'] = 1;
$where ['group_id'] = array(array('exp','is not null'),array('neq',0));
$list = $node->where ( $where )->field ( 'ID,PID,NAME,GROUP_ID,TITLE' )->order ( 'group_id,level_id,sort' )->select ();
$gList = $node->where($where)->getField('id,group_id');
$accessList = $_SESSION ['_ACCESS_LIST'];
foreach ( $list as $key => $module ) {
if (isset ( $accessList [strtoupper ( APP_NAME )] [strtoupper ( $module ['name'] )] ) || $_SESSION ['administrator']) {
//设置模块访问权限
$module ['access'] = 1;
$menu [$key] = $module;
}
}
$groupM = D('Group');
$groupMap['status'] = 1;
$groupMap['id'] = array('in',array_flip(array_flip($gList)));
$groupList = $groupM->where($groupMap)->order('sort asc')->select();
$menu = arrToTree($menu,0);
if (! empty ( $_GET ['tag'] )) {
$this->assign ( 'menuTag', $_GET ['tag'] );
}
//dump($menu);
$this->assign ( 'groupList', $groupList );
$this->assign ( 'menu', $menu );
}
C ( 'SHOW_RUN_TIME', false ); // 运行时间显示
C ( 'SHOW_PAGE_TRACE', false );
$this->display ();
}
模板menu.html:
<div class="accordion" fillSpace="sideBar">
<volist id="v1" name="groupList" >
<div class="accordionHeader">
<h2><span>Folder</span>{$v1.title}</h2>
</div>
<div class="accordionContent">
<ul class="tree treeFolder">
{$v1['id']|outMenu=$menu}
</ul>
</div>
</volist>
</div>
最终效果:
(本文为作者原创,如需转载及引用请标明作者、出处)