繁华2018-08-10 19:34:33
找到錯誤了,add方法裡這個$menus要改成$results
錯誤的 $this->assign('menus',$menus);
#正確的 $this->assign('menus',$results);
繁华2018-08-10 19:29:41
<?php /** 角色管理 */ namespace app\admins\controller; use app\admins\controller\BaseAdmin; class Roles extends BaseAdmin { //角色列表 public function index(){ $data['roles']=$this->db->table('admin_groups')->lists(); $this->assign('data',$data); return $this->fetch(); } //角色添加 public function add(){ $menus_list = $this->db->table('admin_menus')->where(array('status'=>0))->cates('mid'); $menus = $this->gettreeitems($menus_list); $results = array(); foreach ($menus as $value) { $value['children'] = isset($value['children'])?$this->formatMenus($value['children']):false; $results[] = $value; } var_dump($results); $this->assign('menus',$menus); return $this->fetch(); } private function gettreeitems($items){ $tree = array(); foreach ($items as $item){ if(isset($items[$item['pid']])){ $items[$item['pid']]['children'][] = &$items[$item['mid']]; } else{ $tree[] = &$items[$item['mid']]; } } return $tree; } private function formatMenus($items,&$res=array()){ foreach ($items as $item) { if(!isset($item['children'])){ $res[] = $item; }else{ $tem = $item['children']; unset($item['children']); $res[] = $item; $this->formatMenus($tem,$res); } } return $res; } }
繁华2018-08-10 19:28:07
array (size=4) 0 => array (size=9) 'mid' => int 1 'pid' => int 0 'ord' => int 0 'title' => string '管理员管理' (length=15) 'controller' => string '' (length=0) 'method' => string '' (length=0) 'ishidden' => int 0 'status' => int 0 'children' => array (size=4) 0 => array (size=8) 'mid' => int 4 'pid' => int 1 'ord' => int 0 'title' => string '管理员列表' (length=15) 'controller' => string 'Admin' (length=5) 'method' => string 'index' (length=5) 'ishidden' => int 0 'status' => int 0 1 => array (size=8) 'mid' => int 17 'pid' => int 4 'ord' => int 0 'title' => string '角色列表' (length=12) 'controller' => string 'Admin' (length=5) 'method' => string 'test' (length=4) 'ishidden' => int 0 'status' => int 0 2 => array (size=8) 'mid' => int 5 'pid' => int 1 'ord' => int 0 'title' => string '管理员添加' (length=15) 'controller' => string 'Admin' (length=5) 'method' => string 'add' (length=3) 'ishidden' => int 1 'status' => int 0 3 => array (size=8) 'mid' => int 6 'pid' => int 1 'ord' => int 0 'title' => string '管理员保存' (length=15) 'controller' => string 'Admin' (length=5) 'method' => string 'save' (length=4) 'ishidden' => int 1 'status' => int 0 1 => array (size=9) 'mid' => int 2 'pid' => int 0 'ord' => int 0 'title' => string '权限管理' (length=12) 'controller' => string '' (length=0) 'method' => string '' (length=0) 'ishidden' => int 0 'status' => int 0 'children' => boolean false 2 => array (size=9) 'mid' => int 3 'pid' => int 0 'ord' => int 0 'title' => string '系统设置' (length=12) 'controller' => string '' (length=0) 'method' => string '' (length=0) 'ishidden' => int 0 'status' => int 0 'children' => boolean false 3 => array (size=9) 'mid' => int 11 'pid' => int 0 'ord' => int 0 'title' => string '文章管理' (length=12) 'controller' => string '' (length=0) 'method' => string '' (length=0) 'ishidden' => int 0 'status' => int 0 'children' => array (size=1) 0 => array (size=8) 'mid' => int 16 'pid' => int 11 'ord' => int 0 'title' => string '文章列表' (length=12) 'controller' => string 'Article' (length=7) 'method' => string 'index' (length=5) 'ishidden' => int 0 'status' => int 0
這是正常顯示的內容