控制器
<?php
namespace app\admin\controller;
use app\admin\model\Cate as CateModel;
use app\admin\model\Article as ArticleModel;
use app\admin\controller\Common;
class cate extends Common
{
public function lst()
{
$cate=new CateModel();
if(request()->isPost()){
$sorts=input('post.');
foreach ($sorts as $k => $v) {
$cate->update(['id'=>$k,'sort'=>$v]);
}
$this->success('更新排序成功!',url('lst'));
return;
}
$cateres=$cate->catetree();
$this->assign('cateres',$cateres);
return view();
}
public function add(){
$cate=new CateModel();
if(request()->isPost()){
$data=input('post.');
$validate = \think\Loader::validate('Cate');
if(!$validate->scene('add')->check($data)){
$this->error($validate->getError());
}
$add=$cate->save($data);
if($add){
$this->success('添加栏目成功!',url('lst'));
}else{
$this->error('添加栏目失败!');
}
}
$cateres=$cate->catetree();
$this->assign('cateres',$cateres);
return view();
}
public function edit(){
$cate=new CateModel();
if(request()->isPost()){
$data=input('post.');
$validate = \think\Loader::validate('Cate');
if(!$validate->scene('edit')->check($data)){
$this->error($validate->getError());
}
$save=$cate->save($data,['id'=>$data['id']]);
if($save !== false){
$this->success('修改栏目成功!',url('lst'));
}else{
$this->error('修改栏目失败!');
}
return;
}
$cates=$cate->find(input('id'));
$cateres=$cate->catetree();
$this->assign(array(
'cateres'=>$cateres,
'cates'=>$cates,
));
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'); //要删除的当前栏目的id
$cate=new CateModel();
$sonids=$cate->getchilrenid($cateid);
$allcateid=$sonids;
$allcateid[]=$cateid;
foreach ($allcateid as $k=>$v) {
$article=new ArticleModel;
$article->where(array('cateid'=>$v))->delete();
}
if($sonids){
db('cate')->delete($sonids);
}
}
}
模型文件
<?php
namespace app\admin\model;
use think\Model;
class Cate extends Model
{
public function catetree(){
$cateres=$this->order('sort desc')->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;
}
视图文件
<tbody>
{volist name="cateres" id="cate"}
<tr>
<td align="center">{$cate.id}</td>
<td align="center"><input name="{$cate.id}" type="text" style="width:50px; text-align:center;" value="{$cate.sort}"></td>
<td>{if condition="$cate['level'] neq 0"}|{/if}<?php echo str_repeat('-', $cate['level']*8)?>{$cate.catename}</td>
<td align="center">
{if condition="$cate['type'] eq 1"}
文章列表
{elseif condition="$cate['type'] eq 3" }
图片列表
{else /}
单页
{/if}
</td>
<td align="center">
<a href="{:url('edit',array('id'=>$cate['id']))}" class="btn btn-primary btn-sm shiny">
<i class="fa fa-edit"></i> 编辑
</a>
<a href="#" onClick="warning('确实要删除吗', '{:url('del',array('id'=>$cate['id']))}')" class="btn btn-danger btn-sm shiny">
<i class="fa fa-trash-o"></i> 删除
</a>
</td>
</tr>
{/volist}
</tbody>
image.png
新闻视图
<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 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>
修改视图
<form class="form-horizontal" role="form" action="" method="post">
<input type="hidden" name="id" value="{$cates.id}" />
<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>
sql
image.png
网友评论