tp5无限极循环导航菜单
TP5前端模板(首页导航NAV)赋值,无限极循环:
index模块下:
index/model/模型文件:
Cate.php 代码:
<?php namespace app\index\Model; use think\Model; class Cate extends Model { protected $table="wz_cate"; protected $resultSetType = 'collection'; public function catetree() { //模型查询cate表,获取所有栏目名称 $cateres = $this->order('sort asc')->select()->toArray(); //调用递归,把查询的数据写入循环 $data = $this->sort($cateres,$pid = 0); return $data; } public function sort($data,$pid=0) { //定义一个数组,用来存储递归后的数据 $children = []; foreach ($data as $k => $v){ if($v['pid']==$pid){ //查询一级菜单 $children[$v['id']] = $v; //写入进数组 $children[$v['id']]['children'] = $this->sort($data,$v['id']); } } return $children; } }
index模块下,所有控制器都基于Commn.php控制器。
在common.php调用Cate.php模型,使用cate模型查询栏目:
<?php namespace app\index\controller; use think\Controller; use think\Db; use app\index\model\Cate; class Common extends Controller { //加载执行调用函数 public function _initialize() { //无限极分类 $this->getNavCates(); } //无限极 public function getNavCates() { $cate = new Cate(); $cateres = $cate->catetree(); $this->assign('cateres', $cateres); } }
剩下的直接在NAV里面,循环输出:
<ul> {volist name="cateres" id="cate"} <li>{$cate.name}</li> {/volist} </ul>