iami233
iami233
文章120
标签35
分类4
ThinkPHP5实现二级分类功能

ThinkPHP5实现二级分类功能

简单记录记录一下…

数据库

--
-- 表的结构 `sort`
--

CREATE TABLE IF NOT EXISTS `api_sort` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `time` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1 ;

--
-- 表的结构 `info`
--

CREATE TABLE IF NOT EXISTS `api_info` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `sort` int(255) NOT NULL,
  `time` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1 ;

控制器

public function getTree(){
    $cateCount = db('sort')->count();
    $cates = Db::name('sort')->order('id','desc')->field('id,name')->select();
    for($i=0;$i<$cateCount;$i++)
    {
        $cates[$i]['nr'] = db('info')->where(array('sort'=>$cates[$i]['id']))->field('id,name')->select();
        $cates[$i]['count'] = db('info')->where(array('sort'=>$cates[$i]['id']))->count();
    }
    echo json_encode($cates);
}

返回数据

[{
	"id": 2,
	"name": "分类一",
	"nr": [{
		"id": 4,
		"name": "测试"
	}, {
		"id": 5,
		"name": "测试"
	}, {
		"id": 6,
		"name": "测试"
	}],
	"count": 3
}, {
	"id": 1,
	"name": "默认分类",
	"nr": [{
		"id": 1,
		"name": "测试"
	}, {
		"id": 2,
		"name": "测试"
	}, {
		"id": 3,
		"name": "测试"
	}],
	"count": 3
}]
本文作者:iami233
本文链接:https://5ime.cn/thinkphp-nav.html
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可