首先創(chuàng)建數(shù)據(jù)庫(kù):
CREATEDATABASEIFNOTEXISTS`fenlei`DEFAULTcharacterSETutf8COLLATEutf8_general_ci;USE`fenlei`;CREATETABLEIFNOTEXISTS`category`(`catid`mediumint(8)unsignedNOTNULLAUTO_INCREMENTCOMMENT'主鍵',`upid`mediumint(8)unsignedNOTNULLDEFAULT'0'COMMENT'上級(jí)分類catid',`catname`varchar(255)NOTNULLDEFAULT''COMMENT'分類名稱',`displayorder`tinyint(1)NOTNULLDEFAULT'0'COMMENT'顯示順序',`status`tinyint(1)NOTNULLDEFAULT'0'COMMENT'0-關(guān)閉芍殖,1-啟用',
PRIMARYKEY(`catid`)
)ENGINE=InnoDBDEFAULTCHARSET=utf8COMMENT='文章分類';INSERTINTO`category`VALUES('1','0','分類1','0','1'),
('2','0','分類2','0','1'),
('3','0','分類3','0','1'),
('4','0','分類4','0','1'),
('5','0','分類5','0','1'),
('6','1','分類1-1','0','1'),
('7','1','分類1-2','0','1'),
('8','1','分類1-3','0','1'),
('9','1','分類1-4','0','1'),
('10','1','分類1-5','0','1'),
('11','6','分類1-1-1','0','1'),
('12','6','分類1-1-2','0','1'),
('13','6','分類1-1-3','0','1'),
('14','7','分類1-2-1','0','1'),
('15','7','分類1-2-2','0','1'),
('16','11','分類1-1-1-1','0','1'),
('17','11','分類1-1-1-2','0','1');
model
* 這是一個(gè)成熟的模型鉴象,放入相應(yīng)文件夾直接調(diào)用即可
*/namespaceapp\index\model;classCategoryextends\think\Model{/*
* 打開(kāi)冰箱:從數(shù)據(jù)庫(kù)取出所有數(shù)據(jù)阔挠,并放進(jìn)緩存內(nèi)
* 即可獨(dú)立輸出也可以在其他位置調(diào)用
*/publicfunctioncategory_query($catid=0){
cache('category',null);//正式環(huán)境刪除本行可減少一次查詢if(!$result = cache('category')){
$Category =newCategory();
$result = [];foreach($Category->order('displayorder asc,catid asc')->select()as$data){
$result[$data['catid']] = $data;
}
cache('category',$result,0);
}return$catid ? $result[$catid] : $result;//如果傳入單個(gè)分類catid,那么直接返回就行,可用于列表頁(yè)礁蔗,大大降低查詢次數(shù)}/*
* 把大象放進(jìn)冰箱:將第一步得到的數(shù)據(jù)集轉(zhuǎn)化為無(wú)限級(jí)數(shù)組
* 即可獨(dú)立輸出也可以在其他位置調(diào)用
*/publicfunctioncategory_tree($upid=0,$status='0,1'){
$status = is_string($status) ? explode(',', $status) : $status;
$result = [];foreach($this->category_query()as$catid=>$cat){if($upid == $cat['upid'] && in_array($cat['status'],$status)){
$cat['subcat'] =$this->category_tree($cat['catid'],$status);
$result[] = $cat;
}
}return$result;
}/*
* 關(guān)上冰箱門:用于實(shí)際用途突硝,將多級(jí)數(shù)據(jù)傳入螃宙,轉(zhuǎn)化為前端html代碼
* 該html的轉(zhuǎn)化結(jié)果可從第一步中獲取方式不同來(lái)實(shí)現(xiàn)從哪一級(jí)開(kāi)始展示
* 本函數(shù)只是師范函數(shù)蛮瞄,實(shí)際運(yùn)用中只需要修改這個(gè)函數(shù)結(jié)構(gòu)體就能完全實(shí)現(xiàn)仿網(wǎng)易蓋樓效果
*/publicfunctioncategory_html($categorys,$depth=0){
$depth_html = $html ='';for($i=0; $i < $depth; $i++) {
$depth_html .='——';
}foreach($categorysas$data){
'.$data['catid'].'
'.$data['displayorder'].'
'.$depth_html.$data['catname'].'
$html .='';if($data['subcat']){
$html .=$this->category_html($data['subcat'],$depth+1);
}
}return$html;
}
}?>
首先通過(guò)category_query方法獲取到所有分類,
然后通過(guò)category_tree方法將得到的數(shù)據(jù)轉(zhuǎn)化為無(wú)限分類數(shù)組
最后再通過(guò)category_html方法將上面得到的無(wú)限分類數(shù)組輸出為html
控制器Controller
$Category =newCategory;//實(shí)例化類$category_tree = $Category->category_tree();// 獲取整體多級(jí)數(shù)組樹(shù)結(jié)果$this->view->category_list = $Category->category_html($category_tree);//將結(jié)果轉(zhuǎn)化為實(shí)體html并傳值給模板return$this->fetch();
}
}