thinkphp5框架前后端分離項目實現(xiàn)分頁功能的方法分析
本文實例講述了thinkphp5框架前后端分離項目實現(xiàn)分頁功能的方法。分享給大家供大家參考,具體如下:
方法一
利用tp5提供的paginate方法實現(xiàn)自動分頁
參數(shù)
page第幾頁,paginate分頁方法會自動獲取
size 每頁數(shù)量
代碼
/**
* 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;
}
返回數(shù)據(jù)
{
"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": "復(fù)仇者聯(lián)盟I",
"section_title": "第11章 你是睡"
},
{
"id": 1,
"mid": 4,
"book_id": 29,
"chapter_id": 34,
"score": 20,
"create_time": 1598999,
"book_name": "復(fù)仇者聯(lián)盟II",
"section_title": "第11章 你是睡"
}
],
"num": 2,
"total_coin": 50,
"error": 0,
"message": "success"
}
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。
相關(guān)文章
yii2 commands模式以及配置crontab定時任務(wù)的方法
下面小編就為大家?guī)硪黄獃ii2 commands模式以及配置crontab定時任務(wù)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
centos7上編譯安裝php7以php-fpm方式連接apache
這篇文章主要介紹了centos7上編譯安裝php7以php-fpm方式連接apache的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-11-11
Yii2-GridView 中讓關(guān)聯(lián)字段帶搜索和排序功能示例
這篇文章主要介紹了Yii2-GridView 中讓關(guān)聯(lián)字段帶搜索和排序功能示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01
PHP接口類(interface)的定義、特點和應(yīng)用示例
這篇文章主要介紹了PHP接口類(interface),結(jié)合實例形式分析了PHP接口類(interface)的基本功能、定義、特點、用法與相關(guān)注意事項,需要的朋友可以參考下2020-05-05
淺談PHP調(diào)用Webservice思路及源碼分享
NuSoap是PHP環(huán)境下的WebService編程工具,用于創(chuàng)建或調(diào)用WebService。它是一個開源軟件,是完全采用PHP語言編寫的、通過HTTP收發(fā)SOAP消息的一系列PHP類。NuSOAP的一個優(yōu)勢是不需要擴展庫的支持,這種特性使得NuSoap可以用于所有的PHP環(huán)境,不受服務(wù)器安全設(shè)置的影響?!?/div> 2014-06-06
thinkPHP框架實現(xiàn)的無限回復(fù)評論功能示例
這篇文章主要介紹了thinkPHP框架實現(xiàn)的無限回復(fù)評論功能,結(jié)合實例形式簡單分析了thinkPHP實現(xiàn)無限回復(fù)的相關(guān)控制器、視圖操作技巧,需要的朋友可以參考下2018-06-06最新評論

