经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
thinkphp5框架前后端分离项目实现分页功能的方法分析
来源:jb51  时间:2019/10/8 13:15:22  对本文有异议

本文实例讲述了thinkphp5框架前后端分离项目实现分页功能的方法。分享给大家供大家参考,具体如下:

方法一

利用tp5提供的paginate方法实现自动分页

参数

page第几页,paginate分页方法会自动获取

size  每页数量

代码

  1. /**
  2. * Notes:消费记录
  3. * Date: 2019/6/25
  4. * Time: 15:43
  5. * @param Request $request
  6. * @return \think\response\Json
  7. */
  8. public function getMyConsumeLog(Request $request)
  9. {
  10. global $_W;
  11. $size = $request->param('size', 6);
  12. $list = $this->model->getListByMid($_W['user']['id'],$size);
  13. return json(['data' => $list, 'error' => 0, 'message' => 'success']);
  14. }
  15. public function getListByMid($mid,$size = 10){
  16. $res = $this
  17. ->alias('c')
  18. ->field('c.*,b.book_name,b.book_flash,s.section_title')
  19. ->leftJoin('booksection s','c.chapter_id = s.id')
  20. ->leftJoin('book b','s.book_id = b.id')
  21. ->where('c.mid',$mid)
  22. ->order('c.id desc')
  23. ->paginate($size);
  24. return $res;
  25. }
  26.  

返回数据

{
    "data": {
        "total": 1,
        "per_page": 1,
        "current_page": 1,
        "last_page": 1,
        "data": [
            {
                "id": 105,
                "mid": 55,
                "book_id": 31,
                "chapter_id": 46046,
                "score": 27,
                "create_time": 1561447448,
                "book_name": "桃运村支书",
                "book_flash": "https://cdnxiaoshuo.t.com/FiO6TM0N4kpzKB7tqrDko64ZS4H4",
                "section_title": "第29章 康庄大道"
            }
        ]
    },
    "error": 0,
    "message": "success"
}

方法二

利用limit方法

  1. $curr_page = $request->param('page', 1);
  2. $size = $request->param('size', 6);
  3. $list = $consume_model->getListByWhere($curr_page, $size, $where);
  4. $num = $consume_model->getListByWhereCount($where);
  5. return json(['data' => $list,'num' => $num,'error' => 0, 'message' => 'success']);
  6. public function getListByWhere($curr_page,$limit = 10,$where = null){
  7. $res = $this
  8. ->alias('c')
  9. ->field('c.*,b.book_name,s.section_title')
  10. ->leftJoin('booksection s','c.chapter_id = s.id')
  11. ->leftJoin('book b','s.book_id = b.id')
  12. ->where($where)
  13. ->order('c.id desc')
  14. ->limit($limit*($curr_page - 1),$limit)
  15. ->select()
  16. ->toArray();
  17. return $res;
  18. }
  19. public function getListByWhereCount($where = null){
  20. $count = $this
  21. ->alias('c')
  22. ->where($where)
  23. ->count();
  24. return $count;
  25. }
  26.  

返回值

{
    "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相关内容感兴趣的读者可查看jb51专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号