亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Product category recursive deletion

Recursive deletion of product categories

Deletion We have done it before, but it is a little different. Deletion of categories is more complicated. , when we delete the top-level column, we should also delete the categories below the top-level column.

cate controller

 public function del($id){
        $cata=D('cate');
        $childids=$cata->getchild($id);
        $childids=implode(',',$childids);
        if($cata->delete($childids)){
          $this->success('刪除欄目成功!',U('index'));
        }else{
            $this->error('刪除欄目失敗!');
        }
    }

CateModel model layer

 public function getchild($cateid){
        $data=$this->select();
        return $this->getchildids($data,$cateid);
    }
    public function getchildids($data,$cateid){
        static $res=array();
        $res[]=$cateid;
        foreach ($data as $k => $v) {
            if ($v['pid']==$cateid) {
                    $res[]=$v['id'];
                    $this->getchildids($data,$v['id']);

            }
        }
        return array_unique($res);
    }

Let’s explain it hierarchically

$childids=$cata->getchild($id);

Pass the id to the getchild method.

public function getchild($cateid){
        $data=$this->select();
        return $this->getchildids($data,$cateid);
    }

The getchild method here queries all categories after receiving the passed id. Return data and id to getchildids method.

  public function getchildids($data,$cateid){
        static $res=array();
        $res[]=$cateid;
        foreach ($data as $k => $v) {
            if ($v['pid']==$cateid) {
                    $res[]=$v['id'];
                    $this->getchildids($data,$v['id']);

            }
        }
        return array_unique($res);
    }

$res=array();Define an array. $res[]=$cateid; The empty array is used to store id.

foreach traverses the data. When its pid is equal to the current id, it means it is the top level. At this time, the id is stored in $ In the empty array of res[], use recursion again.

return array_unique($res); Eileen returns this array, array_unique removes duplicates.

$childids=implode(',',$childids); Split the array into strings and you can use it.

This is when you delete the top-level column and you will find that you have deleted multiple ones.

Continuing Learning
||
<?php namespace Admin\Controller; use Think\Controller; class CateController extends CommonController { public function index(){ $cate=D('cate'); $cateres=$cate->catetree(); $this->assign('cateres',$cateres);//獲取欄目樹 $this->display(); } public function add(){ $cate=D('cate'); if (IS_POST) { if($cate->create()){ if ($cate->add()) { $this->success('添加商品分類成功',U('Cate/add')); }else{ $this->error('添加商品分類失敗!'); } }else{ $this->error($cate->getError()); } return; } $cateres=$cate->catetree(); $this->assign('cateres',$cateres); $this->display(); } public function edit(){ $cate=D('cate'); if (IS_POST) { if($cate->create()){ if ($cate->save()!== false) { $this->success('修改商品分類成功',U('Cate/index')); }else{ $this->error('修改商品分類失敗!'); } }else{ $this->error($cate->getError()); } return; } $cates=$cate->find(I('id')); $cateres=$cate->catetree(); $this->assign('cateres',$cateres); $this->assign('cates',$cates); $this->display(); } public function del($id){ $cata=D('cate'); $childids=$cata->getchild($id); $childids=implode(',',$childids); if($cata->delete($childids)){ $this->success('刪除欄目成功!',U('index')); }else{ $this->error('刪除欄目失?。?); } } }
submitReset Code