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('删除栏目失败');
                }
            }

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

  • 相关阅读:
    操作系统
    redis
    数据库原理与mysql
    计算机网络
    重写、重载、隐藏以及多态分析
    c++复习重点
    重装系统记录
    正则表达式匹配ip地址
    信号量和互斥锁的区别 互斥量与临界区的区别
    为Markdown文件生成目录
  • 原文地址:https://www.cnblogs.com/leigood/p/7258160.html
Copyright © 2011-2022 走看看