An example of ThinkPHP using less thanvolistgreater than to realize three level loop code
- 2021-07-09 07:17:48
- OfStack
In this paper, an example is given to describe the adoption of ThinkPHP < volist > Label implements 3-level loop code, and the specific operation steps are as follows:
A 3-dimensional array is required for a 1. 3-level loop, and the implementation code is as follows:
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'])){ // First judge whether there is a first 3 Level subclass , The final array is shaped like $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. Bind the volist tag:
$result=D('Learning') ->MakeTree(0,1);
//dump($result);
$this->assign('list',$result);
3. Template section:
<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>