<?php
namespace app\admin\model;
use think\Model;class Cate extends Model
{
   
    public function catetree(){
        $cateres=$this->select();

        return $this->sort($cateres);
    }
   public function sort($data,$pid=0,$level=0){
static $arr=array();
foreach ($data as $k=> $v){
if($v['pid']==$pid){
$v['level']=$level;
$arr[]=$v;
$this->sort($data,$v['id'],$level+1);
}
}
return $arr;
   }
   
    public function getchilrenid($cateid){
     $cateres=$this->select();
return $this->_getchilrenid($cateres,$cateid);
}
public function _getchilrenid($cateres,$cateid){
static $arr=array();
foreach ($cateres as $k=>$v) {
if($v['pid'] == $cateid){
$arr[]=$v['id'];
$this->_getchilrenid($cateres,$v['id']);
}
}
return $arr;
}

}
<?php
namespace app\admin\controller;use app\admin\model\Cate as CateModel;
use app\admin\controller\Common;class cate extends Common
{
    protected $beforeActionList =[
    'delsoncate' => ['only'=>'del'],
    ];
       
public function lst()
    {  // $auth=new Auth();
    $cate=new CateModel();
$cateres=$cate->catetree();

    $this->assign('cateres',$cateres);
    return view();
}

  public function add()
    {  //模型添加 
     $cate=new CateModel();
     if(request()->isPost()){
    
$add=$cate->save(input('post.'));
if($add){
$this->success('添加成功',url('lst'));
}else{
$this->error('添加失败');
}
     }
$cateres=$cate->catetree();
   $this->assign('cateres',$cateres);
//$cateres=$cate->select();
//$this->assign('cateres',$cateres);
        return view();
}


public function edit(){
$cate=new CateModel();
$cates=$cate->find(input('id'));
$cateres=$cate->catetree();
   $this->assign(array(
'cateres'=>$cateres,
'cates'=>$cate,
));
return view();
}



public function del(){
$del=db('cate')->delete(input('id'));
if($del){
$this->success('删除成功',url('lst'));
}else{
$this->error('删除失败!');
}
}
public function delsoncate(){
$cateid=input('id');
$cate=new CateModel();
$sonids=$cate->getchilrenid($cateid);
if($sonids){
db('cate')->delete($sonids);

}

}
}<!DOCTYPE html>
<html><head>
    <meta charset="utf-8">
    <title>童老师ThinkPHP交流群:484519446</title>    <meta name="description" content="Dashboard">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!--Basic Styles-->
    <link href="__STATIC__/admin/style/bootstrap.css" rel="stylesheet">
    <link href="__STATIC__/admin/style/font-awesome.css" rel="stylesheet">
    <link href="__STATIC__/admin/style/weather-icons.css" rel="stylesheet">    <!--Beyond styles-->
    <link id="beyond-link" href="__STATIC__/admin/style/beyond.css" rel="stylesheet" type="text/css">
    <link href="__STATIC__/admin/style/demo.css" rel="stylesheet">
    <link href="__STATIC__/admin/style/typicons.css" rel="stylesheet">
    <link href="__STATIC__/admin/style/animate.css" rel="stylesheet">
    
</head>
<body>
<!-- 头部 -->
{include file="common/top"}
<!-- /头部 -->
{include file="common/left"}

            <!-- /Page Sidebar -->
            <!-- Page Content -->
            <div class="page-content">
                <!-- Page Breadcrumb -->
                <div class="page-breadcrumbs">
                    <ul class="breadcrumb">
                        <li>
                        <a href="__STATIC__/admin/#">系统</a>
                    </li>
                        <li>
                        <a href="__STATIC__/admin/#">栏目管理</a>
                     </li>
                        <li class="active">修改栏目</li>
                    </ul>
                </div>
                <!-- /Page Breadcrumb -->                <!-- Page Body -->
                <div class="page-body">
                    
<div class="row">
    <div class="col-lg-12 col-sm-12 col-xs-12">
        <div class="widget">
            <div class="widget-header bordered-bottom bordered-blue">
                <span class="widget-caption">修改栏目</span>
            </div>
            <div class="widget-body">
                <div id="horizontal-form">
                    <form class="form-horizontal" role="form" action="" method="post">
                                               <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">上级栏目</label>
                            <div class="col-sm-6">
                              <select name="pid">
                               <option value="0">顶级栏目</option>
                               {volist name="cateres" id="cate"}
                               <option {if condition="$cates['pid'] eq $cate['id']"} selected="selected" {/if}value="{$cate.id}">{if condition="$cate['level'] neq 0"} |{/if}<?php echo str_repeat('-',$cate['level']*4)?>{$cate.catename}</option>
                               {/volist}
                              </select>
                            </div>
                              <p class="help-block col-sm-4 red">* 必填</p>
                        </div>  
                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">栏目名称</label>
                            <div class="col-sm-6">
                                <input class="form-control"  placeholder="" value="{$cates.catename}" name="catename" type="text">
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>
                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">栏目名称</label>
                            <div class="col-sm-6">
                            
                             <div class="radio" style="float:left; margin-right:10px;">
                                    <label>
                                        <input {if condition="$cates['type'] eq 1"}cchecked="checked"{/if} name="type" value="1"  type="radio">
                                        <span class="text">列表</span>
                                    </label>
                                </div>
                                <div class="radio" style="float:left; margin-right:10px;">
                                    <label>
                                        <input {if condition="$cates['type'] eq 1"} checked="checked"{/if} name="type" class="inverted" value="2" type="radio">
                                        <span class="text">单页</span>
                                    </label>
                                </div>
                                <div class="radio" style="float:left; margin-right:10px;">
                                    <label>
                                        <input name="type" class="inverted" value="3" type="radio">
                                        <span class="text">图片列表</span>
                                    </label>
                                </div>
                            
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>
                        
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-default">保存信息</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>                </div>
                <!-- /Page Body -->
            </div>
            <!-- /Page Content -->
</div>
</div>     <!--Basic Scripts-->
    <script src="__STATIC__/admin/style/jquery_002.js"></script>
    <script src="__STATIC__/admin/style/bootstrap.js"></script>
    <script src="__STATIC__/admin/style/jquery.js"></script>
    <!--Beyond Scripts-->
    <script src="__STATIC__/admin/style/beyond.js"></script>
    
</body></html>