表结构
CREATE TABLE `s_author` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`auth_name` varchar(50) DEFAULT NULL COMMENT '权限名称',
`auth_model` varchar(50) DEFAULT NULL COMMENT 'url',
`auth_controller` varchar(50) DEFAULT NULL COMMENT '控制器',
`auth_action` varchar(50) DEFAULT NULL COMMENT '方法',
`parent_id` int(11) DEFAULT NULL COMMENT '父级',
`create_id` int(11) DEFAULT NULL,
`create_date` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `s_author` (`id`, `auth_name`, `auth_model`, `auth_controller`, `auth_action`, `parent_id`, `create_id`, `create_date`)
VALUES
(1,'业务管理','','','',0,1,1585794902),
(3,'任务发布','admin','tasks','add',2,1,1585795886),
(5,'发票管理','admin','bill','index',1,NULL,NULL),
(2,'任务管理','admin','tasks','index',1,NULL,NULL),
(4,'分配','admin','tasks','assigntion',2,NULL,NULL);
获取树形数据方法
function get_tree($arr, $pid = 0, $id = 'id', $pname = 'parent_id', $child = 'children',$deep=1){
$tree = array();
foreach ($arr as $value) {
if ($value[$pname] == $pid) {
$value[$child] = get_tree($arr, $value[$id], $id = 'id', $pname = 'parent_id', $child = 'children',$deep+1);
if ($value[$child] == null) {
unset($value[$child]);
}
$value['deep'] = $deep;
$tree[] = $value;
}
}
return $tree;
}
function getTree($array){
$items = array();
foreach($array as $value){
$items[$value['id']] = $value;
}
$tree = array();
foreach($items as $key => $value){
if(isset($items[$value['pid']])){
$items[$value['pid']]['child'][] = &$items[$key];
}else{
$tree[] = &$items[$key];
}
}
return $tree;
}
PHP调用
public function list(){
$data = DB::table('s_author')->select();
$authors = get_tree($data);
$this->assign('authors',$authors);
return $this->fetch();
}
html渲染
<select name="parent_id" id="">
<option value="0">顶级菜单option>
{volist name="authors" id="author"}
<option value="{$author.id}">{$author.auth_name}option>
{volist name="$author['children']" id="auth"}
<option value="{$auth.id}">{$auth.auth_name}option>
{/volist}
{/volist}
select>
效果图
