您的位置:澳门402永利com > 关于计算机 > 实现三级循环代码实例,Infiniti极分类

实现三级循环代码实例,Infiniti极分类

发布时间:2019-09-23 20:45编辑:关于计算机浏览(174)

    本文以实例描述了ThinkPHP采取<volist>标签完毕三级循环代码,具体操作步骤如下:

    .递归达成

    1. 三级循环必要三个维度数组,达成代码如下:

      function MakeTree($pid,$level) { $map['pid'] = $pid; $map['level'] = $level; $result = $this->where($map)->order('rank ASC')->findall(); if($result){ foreach ($result as $key => $value){ $title = $value['alias']; $list[$title]['id'] = $value['id']; $list[$title]['pid'] = $value['pid']; $list[$title]['alias']= $value['alias']; $list[$title]['title'] = $value['title']; $list[$title]['level'] = $value['level']; $list[$title]['state'] = $value['state']; $list[$title]['rank'] = $value['rank']; if($value['level']<=3){ $list[$title]['child'] = $this->_MakeSonTree($value['id']); } } } return $list; }

      function _MakeSonTree($pid) { $map['pid'] = $pid; $result = $this->where($map)->order('rank ASC')->findall(); if($result){ foreach ($result as $key => $value){ $title = $value['alias']; $list[$title]['id']= $value['id']; $list[$title]['pid']= $value['pid']; $list[$title]['alias']= $value['alias']; $list[$title]['title'] = $value['title']; $list[$title]['level'] = $value['level']; $list[$title]['state'] = $value['state']; $list[$title]['rank'] = $value['rank']; if($this->haschild($value['id'])){ //先决断是或不是有第三级子类,最终的数组形如$result['child']['grandchild']; $list[$title]['grandchild']=$this->_MakeSonTree($value['id']); } } } return $list; }

      function haschild($id){ $result=D('LearningChannel')->where("pid=".$id)->find(); if($result){ return true; } else return false; }

    functiongetTree($array, $pid =0, $level = 0){
    //注明静态数组,防止递归调用时,数拾三回声称导致数组覆盖
    **
    static$list = []; foreach($arrayas$key => $value){
    //第叁回遍历,找到父节点为根节点的节点 也便是pid=0的节点
    if($value['pid'] == $pid){
    //父节点为根节点的节点,等级为0,也正是首先级 $value[
    'level'] = $level;
    //把数组放到list中 $list[] = $value;
    //把这一个节点从数组中移除,减弱后续递归消耗
    unset($array[$key]);
    //先导递归,查找父ID为该节点ID的节点,品级则为原品级+1 getTree($array, $value[
    'id'], $level+1);
    }
    }
    return**$list;
    }

    2.绑定volist标签:

    结果如图:

     $result=D('Learning') ->MakeTree(0,1); 
     //dump($result);
     $this->assign('list',$result);
    

    图片 1

    3.模板部分:

    .树结构

    <select name="category" id="select" class="text mr5"> 
     <volist name="list" id="vo"> 
      <option name="cid" value="{$vo.id}" <eq name="vo.id" value="getid">selected</eq> >{$vo.alias}</option>  
     <volist name="vo['child']" id="child"> 
      <option name="cid" value="{$child.id}" <eq name="child.id" value="getid">selected</eq> >--{$child.alias}</option>  
      <volist name="child['grandchild']" id="grand"> 
      <option name="cid" value="{$grand.id}" <eq name="grand.id" value="getid">selected</eq> >---{$grand.alias}</option>  
      </volist> 
     </volist> 
     </volist> 
     </select>
    

    functiongetTree($items,$pid ="pid") {
    $map = [];
    $tree = [];
    foreach($itemsas&$it){ $map[$it['id']] = &$it; } //数据的ID名生成新的援引索引树
    **
    foreach($itemsas&$at){
    $parent = &$map[$at[$pid]];
    if {
    $parent[
    'children'][] = &$at;
    }
    else{
    $tree[] = &$at;
    }
    }
    return**$tree;
    }

    结果如图:

    图片 2

    本文由澳门402永利com发布于关于计算机,转载请注明出处:实现三级循环代码实例,Infiniti极分类

    关键词: