thinkphp5框架前后端分离项目实现分页功能的方法(2)

方法二

利用limit方法

$curr_page = $request->param('page', 1);
    $size = $request->param('size', 6);
$list = $consume_model->getListByWhere($curr_page, $size, $where);
    $num = $consume_model->getListByWhereCount($where);
    return json(['data' => $list,'num' => $num,'error' => 0, 'message' => 'success']);
public function getListByWhere($curr_page,$limit = 10,$where = null){
    $res = $this
      ->alias('c')
      ->field('c.*,b.book_name,s.section_title')
      ->leftJoin('booksection s','c.chapter_id = s.id')
      ->leftJoin('book b','s.book_id = b.id')
      ->where($where)
      ->order('c.id desc')
      ->limit($limit*($curr_page - 1),$limit)
      ->select()
      ->toArray();
    return $res;
}
public function getListByWhereCount($where = null){
    $count = $this
      ->alias('c')
      ->where($where)
      ->count();
    return $count;
}

返回值

{
    "data": [
        {
            "id": 2,
            "mid": 4,
            "book_id": 4,
            "chapter_id": 22,
            "score": 30,
            "create_time": 0,
            "book_name": "复仇者联盟I",
            "section_title": "第11章  你是睡"
        },
        {
            "id": 1,
            "mid": 4,
            "book_id": 29,
            "chapter_id": 34,
            "score": 20,
            "create_time": 1598999,
            "book_name": "复仇者联盟II",
            "section_title": "第11章  你是睡"
        }
    ],
    "num": 2,
    "total_coin": 50,
    "error": 0,
    "message": "success"
}

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/5628.html