PHP获取多级树形菜单,无限极分类树形菜单

level代表菜单级别,前端根据此控制缩进

pid  上级分类id

代码

PHP 

查询数据库中所有菜单,进行排序

public function getCatalogTree()
    {
        $list = $this->select();
        return $this->sort($list);
    }

    public function sort($data, $pid = 0, $level = 0)
    {
        static $arr = array();
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                $v['level'] = $level;
                $arr[] = $v;
                $this->sort($data, $v['id'], $level + 1);
            }
        }
        return $arr;
    }
    /**
     * 缩进
     */
    function topString(level) {
        var str = '';
        for (var i=0; i

 

你可能感兴趣的:(php,thinkPHP,mysql)