zoukankan      html  css  js  c++  java
  • 夺命雷公狗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('删除栏目失败');
                }
            }

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

  • 相关阅读:
    layui flow loading占位图实现方法
    layui弹出层layer的area过大被遮挡
    layui 复选框checkbox 实现全选全选
    axios 设置headers token
    elementUI vue this.$confirm 和el-dialog 弹出框 移动
    vue + axios + formdata 上传文件带参数的爬坑之路
    Java四舍五入时保留指定小数位数
    List containsKey 和Map contains 判断集合中是否包含某个值
    BigDecimal 基本使用 比较大小和加减乘除
    springMVC返回json数据乱码问
  • 原文地址:https://www.cnblogs.com/leigood/p/7258160.html
Copyright © 2011-2022 走看看