欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ThinkPHP采用实现三级循环代码实例

程序员文章站 2022-10-06 15:15:11
本文以实例描述了thinkphp采用标签实现三级循环代码,具体操作步骤如下: 1. 三级循环需要三维数组,实现代码如下: funct...

本文以实例描述了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; 
}

2.绑定volist标签:

 $result=d('learning') ->maketree(0,1); 
 //dump($result);
 $this->assign('list',$result);

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>