程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> ThinkPHP采用<volist>實現三級循環代碼實例

ThinkPHP采用<volist>實現三級循環代碼實例

編輯:PHP綜合

本文以實例描述了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>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved