HTML页面:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
li{
list-style: none;
}
</style>
</head>
<body>
<div>
<ul>
{volist name="res" id="v"}
<li class="yi">{$v.power_name}</li>
<div>
<ul>
{volist name="v.son" id="vv"}
<li><a href='{:url("$vv[controller_name]"."/"."$vv[action_name]")}'>{$vv.power_name}</a></li>
{/volist}
</ul>
</div>
{/volist}
</ul>
</div>
<script src="__STATIC__/js/jquery.min.js"></script>
<script>
$(document).on("click",".yi",function () {
$(this).next("div").slideToggle()
})
</script>
</body>
</html>
PHP页面:
<?php
namespace appindexcontroller;
use thinkController;
use thinkDb;
use thinkSession;
class Welcome extends Controller
{
//主页面
public function index()
{
$user=Session::get("user",'think');
$u_id=$user['u_id'];
$data=Db::query("select * from power where power_id in(select powerid from role_power where role_id in(select role_id from user_role where user_id='$u_id'))");
Session::set("data",$data,'think');
$res=$this->digui($data,$p_id=0);
return view("show",['res'=>$res]);
}
//递归
public function digui($data,$p_id=0)
{
$child=[];
foreach ($data as $k=>$v)
{
if($v['p_id']==$p_id)
{
$child[]=$v;
}
}
//二级
foreach ($child as $kk=>$vv)
{
$res=$this->digui($data,$vv['power_id']);
$child[$kk]['son']=$res;
}
//返回值
return $child;
}
}