thinkphp之无限分类
namespace Home\Controller; use Home\Controller; class CategoryController extends Controller { //无限分类读取导航 protected function getCategory() { //实例化导航模型 $category = M("category"); //定义并且执行SQL,返回结果为数组 $sql="SELECT title,catid,catname,listorder,ismean,arrparentid,concat(arrparentid,',',catid) as fullpath FROM `tp_category` as a,`tp_content_type` as b where a.modelid = b.id ORDER BY fullpath ASC;"; $data = $category->query($sql); //定义空数组$result用来接收计算出层阶后的结果 $result = array(); foreach($data as $v) { $deep = count(explode(',',trim($v['fullpath'],','))); $v['catname'] = str_repeat(' ',($deep-1)*3).'|--'.$v['catname']; $result[] = $v; } //将结果以二维数组返回 return $result; } //栏目列表 public function index() { //调用无限分类读取导航方法 $data = $this->getCategory(); //模版赋值 $this->assign("data",$data); //调用模板 $this->display(); } }
CREATE TABLE IF NOT EXISTS `tp_category` ( `catid` int(4) NOT NULL AUTO_INCREMENT COMMENT '栏目ID', `catname` varchar(255) NOT NULL COMMENT '栏目名称', `url` varchar(255) NOT NULL COMMENT '栏目URL', `type` tinyint(1) NOT NULL DEFAULT '0' COMMENT '栏目类型', `modelid` tinyint(1) NOT NULL DEFAULT '0' COMMENT '模型ID', `parentid` int(4) NOT NULL COMMENT '父ID', `arrparentid` varchar(255) NOT NULL COMMENT '所有父类ID', `issoncatid` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否有子栏目', `listorder` int(4) NOT NULL DEFAULT '0' COMMENT '显示顺序', `ismean` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否显示,1表示显示;0表示隐藏', PRIMARY KEY (`catid`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='栏目表' AUTO_INCREMENT=36 ; -- -- 转存表中的数据 `tp_category` -- INSERT INTO `tp_category` (`catid`, `catname`, `url`, `type`, `modelid`, `parentid`, `arrparentid`, `issoncatid`, `listorder`, `ismean`) VALUES (1, '最新资讯', '/Home/News/newsList', 1, 2, 0, '0', 0, 1, 0), (2, '特色主题游学', '/Home/Characteristic', 1, 5, 0, '0', 1, 2, 0), (3, '商学院海外访学', '/Home/Business', 1, 5, 0, '0', 1, 3, 0), (4, '出国访学', '/Home/Goabroad', 1, 5, 0, '0', 1, 4, 0), (5, '精彩视图', '/Home/Marvellous', 1, 3, 0, '0', 1, 5, 0), (6, '营地风采', '/Home/Camp', 1, 2, 0, '0', 1, 6, 0), (7, '专业答疑', '/Home/Answer', 1, 2, 0, '0', 0, 7, 0), (8, '青少年特色游学', '/home/Teenagers', 1, 5, 0, '0', 0, 8, 0), (9, '金融类海外研修', '/Home/Finance', 1, 5, 0, '0', 0, 9, 0), (10, '美加路线', '/Home/Characteristic/list', 1, 5, 20, '0,2,20', 0, 1, 0), (11, '澳洲路线', '/Home/Characteristic/list', 1, 5, 20, '0,2,20', 0, 2, 0), (12, '欧洲路线', '/Home/Characteristic/list', 1, 5, 20, '0,2,20', 0, 3, 0), (13, '亚洲路线', '/Home/Characteristic/list', 1, 5, 20, '0,2,20', 0, 4, 0), (14, '精彩照片', '/Home/Marvellous/photo', 1, 3, 5, '0,5', 0, 1, 0), (15, '精彩视频', '/Home/Marvellous/video', 1, 4, 5, '0,5', 0, 2, 0), (16, '北美', '/Home/Camp/northAmerica', 1, 2, 6, '0,6', 0, 1, 0), (17, '欧洲', '/Home/Camp/europe', 1, 2, 6, '0,6', 0, 2, 0), (18, '澳洲', '/Home/Camp/australia', 1, 2, 6, '0,6', 0, 3, 0), (19, '亚洲', '/Home/Camp/asia', 1, 2, 6, '0,6', 0, 4, 0), (20, '本季特色游学', '/Study/', 1, 5, 2, '0,2', 1, 1, 0), (21, '体育类专题游学', '/Study', 1, 5, 2, '0,2', 1, 2, 0), (22, '足球特长', '/Study', 1, 5, 21, '0,2,21', 0, 1, 0), (23, '篮球特长', '/Study', 1, 5, 21, '0,2,21', 0, 3, 0), (24, '艺术类专题游学', '/Study', 1, 1, 2, '0,2', 0, 3, 0), (25, 'MBA', '/Study', 1, 5, 3, '0,3', 0, 1, 0), (26, 'EMBA', '/Study', 1, 5, 3, '0,3', 0, 2, 0), (27, '美国', '/Study', 1, 5, 3, '0,3', 0, 3, 0), (28, '英国', '/Study', 1, 5, 3, '0,3', 0, 4, 0), (29, '欧洲', '/Study', 1, 5, 3, '0,3', 0, 5, 0), (30, '以色列', '/Study', 1, 5, 3, '0,3', 0, 6, 0), (31, '美国', '/S', 1, 5, 4, '0,4', 0, 0, 0), (32, '英国', '/Study', 1, 5, 4, '0,4', 0, 2, 0), (33, '澳洲', '/Study', 1, 5, 4, '0,4', 0, 3, 0), (34, '加拿大', '/Study', 1, 5, 4, '0,4', 0, 4, 0), (35, '留学直通车', '/Study', 1, 5, 4, '0,4', 0, 5, 0);
Field | Type | Null | Key | Default | Extra |
-------------+--------------+------+-----+---------+----------------+
catid | int(4) | NO | PRI | NULL | auto_increment |
catname | varchar(255) | NO | | NULL | |
url | varchar(255) | NO | | NULL | |
type | tinyint(1) | NO | | 0 | |
modelid | tinyint(1) | NO | | 0 | |
parentid | int(4) | NO | | NULL | |
arrparentid | varchar(255) | NO | | NULL | |
issoncatid | tinyint(1) | NO | | 0 | |
listorder | int(4) | NO | | 0 | |
ismean | tinyint(1) | NO | | 1 | |
-------------+--------------+------+-----+---------+----------------+