tp5无限极循环导航菜单

TP5前端模板(首页导航NAV)赋值,无限极循环:

index模块下:

index/model/模型文件:

 

Cate.php 代码:

<?php
namespace appindexModel;
use thinkModel;
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 appindexcontroller;

use thinkController;
use thinkDb;
use appindexmodelCate;

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>

  

原文地址:https://www.cnblogs.com/web928943/p/13703702.html