效果图如下

ThinkPHP菜单无极分类实例讲解

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>
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。