夺命雷公狗TP3.2.3商城17-----无限极分类删除(玩法2:栏目和栏目下的子级栏目都删除)...

首先我们来到模版下对Cate的模型下进行添加两个函数,使用递归的方法将他的子级栏目的id取出来:

<?php
namespace Admin\Model;
use Think\Model;
class CateModel extends Model {
    protected $_validate = array(
        array('catename','require','分类名称不能为空',1)
    );
    //定义一个方法,获取树状的分类信息
    public function cateTree(){
        $cats = $this->order('id desc')->select();
        //通过tree里面的方法进行排序
        return $this->tree($cats);
    }
    public function tree($arr,$pid=0,$level=0){ //$level是自己定义出来的
        static $tree = array();
        foreach($arr as $v){
           if($v['pid']==$pid){
                //说明找到,保存
                $v['level'] = $level;
                $tree[] = $v;
                //继续找
                $this -> tree($arr,$v['id'],$level+1); //这里的cate_id表的id
            }
        }
        return $tree;
    }


    //这里相当于一个删除栏目的中间件
    public function getchild($cateid){
        $data = $this -> select();
        return $this -> getchildres($data,$cateid);
    }
    //这里直接使用递归删除
    public function getchildres($data,$cateid){
        static $ret = array();
        $ret[] = $cateid;
        foreach($data as $k => $v){
            if($v['pid'] == $cateid){
                $ret[] = $v['id'];
                $this -> getchildres($data,$v['id']);
            }
        }
        return array_unique($ret);//array_unique主要是用于去除重复的
    }
}

 

然后我们来控制器下进行编辑del方法:

 

public function del(){
            $mod = D('cate');
            $id = I('id');
            $getchildres = $mod -> getchild($id);
            $getchildres = implode(',', $getchildres); //implode数组用,号分割组装成字符串
            //dump($getchildres);die;
            if($mod -> delete($getchildres)){
                $this -> success('删除栏目成功');
            }else{
                $this -> error('删除栏目失败');
            }
        }

 

这样即可实现传说中的霸道删除了,嘻嘻。。。

转载于:https://www.cnblogs.com/leigood/p/7258160.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值