• 筛选分类列表展示


    接着昨天未完成的部分,下面接着实现,当筛选之后下面对应出现相关类型的内容。话不多说,先上效果图。

        数据库设计:

        文章表一张:art;

     文章表的sortid字段关联分类表的id,type_id关联type表字段。填充数据大致如下,如果不清楚sortid,和type_id请移步至昨天我的另一篇博客《php实现类似慕课网,php中文网的分类功能 》

    后端代码:

    //文章列表
    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();
    //得到所有的大分类一维数组
    $list = array();
    foreach ($sortList as $item =>&$value){
    $list[] = $value['id'];
    }
    if(in_array($sort,$list)){
    //判断穿过来的分类参数是否在大分类里面
    //拿到该大分类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);
    //判断此时类型是否为空
    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;
    }
    代码讲解:在首页列表多加一个参数artList,通过传的$sort,$parent,$type三个参数来筛选对应的文章。代码部分我都进行了详细的注释,请看上面写的artList方法。
    前端页面 采用的是bootstrap,昨天忘了说,页面的代码很简单就是一个列表的展示,用到了thinkphp的标签,代码如下。

    整体的效果给大家展示一下:

    
    

     

       

  • 相关阅读:
    变量与作用域
    安装node和grunt
    神奇的万维网
    大小写字母的转换
    跨域的方法
    选择器中含有空格的注意事项
    Tools
    jquery中innerWidth(),outerWidth(),outerWidth(true)和width()的区别
    网页中的foot底部定位问题
    CSS hack
  • 原文地址:https://www.cnblogs.com/qiaoliang151715/p/9842695.html
Copyright © 2020-2023  润新知