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


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

    <?php
    namespace AdminModel;
    use ThinkModel;
    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('删除栏目失败');
                }
            }

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

  • 相关阅读:
    【转载】消息队列使用的四种场景介绍
    Vue项目部署打包
    创建Vue项目vue-cli &#183; Failed to download repo vuejs-templates/webpack: connect ETIMEDOUT
    数据库错误:ORA-12154
    oracle数据库一条sql语句批量插入数据
    Linux time scap
    winscp不能使用root登录
    Linux find ./ -name *.bak | xargs rm -rf
    Linux命令echo 3 > /proc/sys/vm/drop_caches
    redis查看服务器占用端口
  • 原文地址:https://www.cnblogs.com/leigood/p/7258160.html
Copyright © 2020-2023  润新知