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

商品分類遞歸刪除

商品分類遞歸刪除

刪除我們之前也做過(guò),但是又有些不同,分類的刪除更為復(fù)雜點(diǎn),我們?cè)趧h除頂級(jí)欄目的時(shí)候應(yīng)該連同頂級(jí)欄目下面的分類也一起刪除。

cate控制器

 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('刪除欄目失?。?#39;);
        }
    }

CateModel模型層

 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);
    }

我們來(lái)分層次的來(lái)講解

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

把id傳給getchild方法。

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

這里getchild方法接受到傳來(lái)的id后查詢所有的分類。返回?cái)?shù)據(jù)和id到getchildids方法。

  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();定義一個(gè)數(shù)組。$res[]=$cateid;空數(shù)組用來(lái)存儲(chǔ)id.

foreach 去遍歷數(shù)據(jù),當(dāng)他的pid等于當(dāng)前id的時(shí)候說(shuō)明是頂級(jí),這時(shí)候吧id存入到$res[]這個(gè)空數(shù)組中,再次用遞歸調(diào)用。

return array_unique($res);是Eileen返回這個(gè)數(shù)組,array_unique去重。

$childids=implode(',',$childids);把數(shù)組分割成字符串就可以使用了。

這是刪除頂級(jí)欄目就能發(fā)現(xiàn)刪除的是多個(gè)了.

繼續(xù)學(xué)習(xí)
||
<?php namespace Admin\Controller; use Think\Controller; class CateController extends CommonController { public function index(){ $cate=D('cate'); $cateres=$cate->catetree(); $this->assign('cateres',$cateres);//獲取欄目樹(shù) $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('刪除欄目失敗!'); } } }
提交重置代碼