Home  >  Article  >  Backend Development  >  Use PHP to implement filtering classification list

Use PHP to implement filtering classification list

little bottle
little bottleforward
2019-04-23 17:32:257815browse

In this article, the editor will share with you how to use PHP to filter and classify lists. It has certain reference value. Friends in need can learn about it. I hope it can be helpful to you.

## Database design:

Article table: art;

## The sortid field of the article table is associated with the id of the classification table, and type_id is associated with the type table field. The filled data is roughly as follows. If you don't know the sortid, and type_id, please go to my other blog yesterday "php implements classification functions similar to MOOC.com and php Chinese website"

Back-end code:

//文章列表
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方法。
The front-end page uses bootstrap. I forgot to mention it yesterday. The code of the page is very simple, just a list display. The thinkphp tag is used. The code is as follows:

Let me show you the overall effect:

<br>

  

Related tutorials:

PHP video tutorial

The above is the detailed content of Use PHP to implement filtering classification list. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete