首页 > 编程技术 > php

ThinkPHP菜单无极分类实例讲解

发布时间:2021-3-5 20:48

效果图如下

controller控制器代码: 

  /**
  * 菜单列表
  */
  public function index(){ 
		$menuList= Db::name('menu')->order('sort,id')->select();
		//递归排序
		$menuList= $this->sort($menuList);
		$this->assign('menuList',$menuList);	
    return view();	
  }
	protected function sort($data,$pid=0,$level=0){
		//此处数据必须是静态数组,不然递归的时候每次都会声明一个新的数组
		static $arr = array();
    foreach ($data as $key=>$value){
      if($value['pid'] == $pid){
        $value["level"]=$level;
        $arr[]=$value;
				//unset()用于销毁指定的变量
				unset($this->data[$key]);
        $this->sort($data,$value['id'],$level+1);
      }
    }
    return $arr;
  }

html模板代码:

<tbody>
{volist name="menuList" id="vo" key="index"}
  <tr>
    <td class="text-left">
    <?php 
      if($vo['pid']!=0) 
        echo str_repeat("&nbsp;",$vo["level"]*3).'├╌ ' 
        /*str_repeat()函数把字符串重复指定的次数。*/ 
    ?>
    {$vo.name}
    </td>						
  </tr>
{/volist}
</tbody>

到此这篇关于ThinkPHP菜单无极分类实例讲解的文章就介绍到这了,更多相关ThinkPHP菜单无极分类内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

标签:[!--infotagslink--]

您可能感兴趣的文章: