ThinkPHP采用实现三级循环代码实例_PHP
程序员文章站
2022-06-16 23:40:27
...
本文以实例描述了ThinkPHP采用标签实现三级循环代码,具体操作步骤如下:
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']_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.模板部分:
推荐阅读
-
ThinkPHP采用
实现三级循环代码实例 -
ThinkPHP采用
实现三级循环代码实例_PHP -
ThinkPHP采用原生query实现关联查询left join实例_php实例
-
ThinkPHP模板Volist标签嵌套循环输出多维数组的方法_php实例
-
ThinkPHP实现批量删除数据的代码实例_PHP
-
ThinkPHP模板循环输出Volist标签用法实例详解_php实例
-
ThinkPHP实现批量删除数据的代码实例_PHP
-
ThinkPHP实现批量删除数据的代码实例_PHP教程
-
合并ThinkPHP配置文件以消除代码冗余的实现方法_php实例
-
采用ThinkPHP中F方法实现快速缓存实例_PHP教程