thinkphp多级分类
核心代码类(放在了Common下的Common文件夹下):
namespace Common\Common; class Tool { static public $treeList = array(); //存放无限分类结果如果一页面有多个无限分类可以使用 Tool::$treeList = array(); 清空 /** * 无限级分类 * @access public * @param Array $data //数据库里获取的结果集 * @param Int $pid //父类id * @param Int $level //第几级分类 * @param Int $sign //符号 * @return Array $treeList */ static public function tree(&$data,$pid = 0,$level = 0,$sign=' ┣ ') { foreach ($data as $key => $value){ if($value['pid']==$pid){ $value['level']=$level+1; $value['sign']=str_repeat($sign,$level); self::$treeList []=$value; self::tree($data,$value['id'],$level+1,$sign); } } return self::$treeList ; } }
控制器调用用于页面输出:
//管理列表 public function index(){ $menu = M('Menu'); $list = $menu->order('pid asc,sort asc')->select(); $this->assign('list',Tool::tree($list)); $this->display(); }
上面要引用:use Common\Common\Tool;
html输出代码:
<volist name="list" id="vo" empty="暂时没有数据"> {$vo.sign}{$vo.name} </volist>
显示效果: