本文实例讲述了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"
}
更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]