欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

yii框架無限極分類的實(shí)現(xiàn)方法

 更新時(shí)間:2017年04月08日 08:49:51   作者:班尼  
這篇文章主要為大家詳細(xì)介紹了yii框架無限極分類的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

用yii框架做了一個(gè)無限極分類,主要的數(shù)組轉(zhuǎn)換都是粘貼的別人的代碼,但還是不要臉的寫出來,方便以后自己看

用的是遞歸,不是path路徑

控制器:

protected function subtree($arr,$id=0,$lev=1){
    $subs = array(); // 子孫數(shù)組
    foreach($arr as $v) {
      if($v['parent_id'] == $id) {
        $v['lev'] = $lev;
        $subs[] = $v; // 舉例說找到array('id'=>1,'name'=>'安徽','parent'=>0),
        $subs = array_merge($subs,$this->subtree($arr,$v['cat_id'],$lev+1));
      }
    }
    return $subs;
  }

public function actionCreate()
  {
    $model = new EcsCategory();
    $query = new \yii\db\Query();
    $query->select('*')
      ->from('ecs_category');
    $command = $query->createCommand();
    $res=$command->queryAll();
    $tree = $this->subtree($res,0,1);
    foreach($tree as $k=> $v) {
      $tree[$k]['new_cat_name']=str_repeat('--',$v['lev']).$v['cat_name'].str_repeat('--',$v['lev']); //str_repeat — 重復(fù)一個(gè)字符串
    }
    $arr=array(
      'new_cat_name'=>'頂級分類',
      'cat_id'=>0
    );
    array_unshift($tree,$arr);
    if ($model->load(Yii::$app->request->post()) && $model->save()) {
      return $this->redirect(['view', 'id' => $model->cat_id]);
    } else {
      return $this->render('create', [
        'model' => $model,
        'data'=>$tree,
      ]);
    }
  }

視圖:

 use \yii\helpers\ArrayHelper;

<?= $form->field($model, 'parent_id')->dropDownList(ArrayHelper::map($data,'cat_id','new_cat_name') ,['prompt' => '請選擇父級分類']) ?>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論