2

ThinkPHP5实现二级分类功能

 2 years ago
source link: https://5ime.cn/thinkphp-nav.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
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
}]

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK