thinkphp5框架前后端分离项目实现分页功能的方法…
2019-10-08 08:54:07来源:编程学习网 阅读 ()
本文实例讲述了thinkphp5框架前后端分离项目实现分页功能的方法。分享给大家供大家参考,具体如下:
方法一
利用tp5提供的paginate方法实现自动分页
参数
page第几页,paginate分页方法会自动获取
size 每页数量
代码
/**
* Notes:消费记录
* Date: 2019/6/25
* Time: 15:43
* @param Request $request
* @return \think\response\Json
*/
public function getMyConsumeLog(Request $request)
{
global $_W;
$size = $request->param('size', 6);
$list = $this->model->getListByMid($_W['user']['id'],$size);
return json(['data' => $list, 'error' => 0, 'message' => 'success']);
}
public function getListByMid($mid,$size = 10){
$res = $this
->alias('c')
->field('c.*,b.book_name,b.book_flash,s.section_title')
->leftJoin('booksection s','c.chapter_id = s.id')
->leftJoin('book b','s.book_id = b.id')
->where('c.mid',$mid)
->order('c.id desc')
->paginate($size);
return $res;
}
返回数据
{
"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方法
$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"
}
原文链接:http://www.phpxs.com/post/6607/
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:PHP进阶学习之垃圾回收机制详解
- 后端开发PHP入门必备 2019-09-30
- PHP主流框架优缺点汇总 2019-09-30
- PHP四大主流框架的优缺点,第一个最难 2019-09-17
- 常用的PHP开发框架,为什么都喜欢用第七个? 2019-09-17
- ThinkPHP5结合Swoole开发实现WebSocket在线聊天 2019-09-08
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
