thinkphp5框架前后端分離項(xiàng)目實(shí)現(xiàn)分頁(yè)功能的方法分析
本文實(shí)例講述了thinkphp5框架前后端分離項(xiàng)目實(shí)現(xiàn)分頁(yè)功能的方法。分享給大家供大家參考,具體如下:
方法一
利用tp5提供的paginate方法實(shí)現(xiàn)自動(dòng)分頁(yè)
參數(shù)
page第幾頁(yè),paginate分頁(yè)方法會(huì)自動(dòng)獲取
size 每頁(yè)數(shù)量
代碼
/**
* Notes:消費(fèi)記錄
* 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;
}
返回?cái)?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": "桃運(yùn)村支書",
"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)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP實(shí)現(xiàn)生成唯一編號(hào)(36進(jìn)制的不重復(fù)編號(hào))
這篇文章主要介紹了PHP實(shí)現(xiàn)生成唯一編號(hào),文中使用10進(jìn)制轉(zhuǎn)換36進(jìn)制得到不重復(fù)的6000多萬(wàn)個(gè)唯一編號(hào),編號(hào)位數(shù)為10位,需要的朋友可以參考下2014-07-07
PHP MYSQL簡(jiǎn)易交互式站點(diǎn)開發(fā)
這篇文章主要為大家詳細(xì)介紹了PHP MYSQL簡(jiǎn)易交互式站點(diǎn)開發(fā),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
thinkPHP5實(shí)現(xiàn)數(shù)據(jù)庫(kù)添加內(nèi)容的方法
這篇文章主要介紹了thinkPHP5實(shí)現(xiàn)數(shù)據(jù)庫(kù)添加內(nèi)容的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了thinkPHP5數(shù)據(jù)庫(kù)的配置、模型、控制器的使用及數(shù)據(jù)插入相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
ThinkPHP有變量的where條件分頁(yè)實(shí)例
這篇文章主要介紹了ThinkPHP有變量的where條件分頁(yè)方法,實(shí)例講述了ThinkPHP條件查詢與分頁(yè)的技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11
PHP控制前臺(tái)彈出對(duì)話框的實(shí)現(xiàn)方法
本文給大家分享通過(guò)php echo出javascript腳本來(lái)控制前臺(tái)彈出對(duì)話框的效果,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看下吧2016-08-08
PHP抓屏函數(shù)實(shí)現(xiàn)屏幕快照代碼分享
誰(shuí)說(shuō)抓圖只能用QQ、用打印屏幕,PHP也能做到,本文主要介紹PHP抓屏函數(shù)實(shí)現(xiàn)屏幕快照的方法2014-01-01

