ThinkPHP 根据请求参数进行组合查询

文章目录

    例如,默认是查询 status 为 1 的文章;

    当请求参数中,包含分类 ID 时,查询条件再加上 category_id。

    $where = array(
        'status' => 1,
    );
    if ($category_id != -1) {
        $where['category_id'] = $category_id;
    }
    
    $news = M("article")
        ->where($where)
        ->select();