筛选分类列表展示
?
接著昨天未完成的部分,下面接著實(shí)現(xiàn),當(dāng)篩選之后下面對(duì)應(yīng)出現(xiàn)相關(guān)類型的內(nèi)容。話不多說(shuō),先上效果圖。
數(shù)據(jù)庫(kù)設(shè)計(jì):
文章表一張:art;
?
?文章表的sortid字段關(guān)聯(lián)分類表的id,type_id關(guān)聯(lián)type表字段。填充數(shù)據(jù)大致如下,如果不清楚sortid,和type_id請(qǐng)移步至昨天我的另一篇博客《php實(shí)現(xiàn)類似慕課網(wǎng),php中文網(wǎng)的分類功能?》
后端代碼:
//文章列表private function artList($type,$direct,$sort,$order="Art.readtime"){
if(!empty($type)){
$where['type_id'] = $type;
}
//找的是大分類下面的小分類
if(!empty($direct) && !empty($sort)){
$where['Sort.id'] = $sort;
}else{
//查詢所有的大分類
$sortList = M("Sort")->where("parent=0")->field("id")->select();
//得到所有的大分類一維數(shù)組
$list = array();
foreach ($sortList as $item =>&$value){
$list[] = $value['id'];
}
if(in_array($sort,$list)){
//判斷穿過(guò)來(lái)的分類參數(shù)是否在大分類里面
//拿到該大分類ID的所有子分類ID
$idList = D("Sort")->where("parent=$sort")->field("id")->select();
if($idList){
$idArr = array();
foreach ($idList as $item =>&$value){
$idArr[] = $value['id'];
}
$ids = implode(",",$idArr);
unset($where);
//判斷此時(shí)類型是否為空
if(!empty($type)){
$where = "Art.sortid in ($ids) and Art.type_id=$type";
}else{
$where = "Art.sortid in ($ids)";
}
}else{
$where['Sort.id']= $sort;
}
}
}
$model = $this->Model= "Art";
$list = D($model)
->where($where)
->field("Art.*")
->join("Sort on Sort.id=Art.sortid")
->order($order)
->select();
return $list;
}
代碼講解:在首頁(yè)列表多加一個(gè)參數(shù)artList,通過(guò)傳的$sort,$parent,$type三個(gè)參數(shù)來(lái)篩選對(duì)應(yīng)的文章。代碼部分我都進(jìn)行了詳細(xì)的注釋,請(qǐng)看上面寫的artList方法。
前端頁(yè)面 采用的是bootstrap,昨天忘了說(shuō),頁(yè)面的代碼很簡(jiǎn)單就是一個(gè)列表的展示,用到了thinkphp的標(biāo)簽,代碼如下。
?
整體的效果給大家展示一下:
?
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/qiaoliang151715/p/9842695.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
- 上一篇: [Swift]LeetCode551.
- 下一篇: 必备软件