smhnaqvi / pagination
一个简单的库,用于轻松地使用json格式创建REST API响应的分页
dev-main
2021-10-12 13:14 UTC
Requires
- php: >=7.4
- another-vendor/package: 1.*
This package is auto-updated.
Last update: 2024-09-12 19:45:16 UTC
README
一个简单的库,用于轻松创建REST API的JSON响应分页
使用composer安装包
composer require smhnaqvi/json-response
或者
从GitHub仓库克隆
git clone git@github.com:smhnaqvi/php-json-response.git
使用方法
use Libraries\Pagination\Pagination; // create Pagination instance // argument 1 has number of total records and accept integer value // argument 2 is current page number by default its 1 integer value // argument 3 is number of how many showing result per page and by default its 10 integer value $records = 52; $currentPage = 6; $showPerPage = 3; $paging = new Pagination($records, $currentPage, $showPerPage);
::paginate
返回分页 对象
$paginate = $paging->paginate(); $total = $paginate->total; $current_page = $paginate->current_page; $all_pages = $paginate->all_pages; $per_page = $paginate->per_page; $has_pagination = $paginate->has_pagination;
::toJson
显示分页最终结果,并使用 exit()
停止PHP进程,使用 json_encode()
将数据编码为JSON。
$paging->toJson();
::toArray
返回分页对象的 数组
$pageArr = $paging->toArray(); $total = $pageArr["total"]; $current_page = $pageArr["current_page"]; $all_pages = $pageArr["all_pages"]; $per_page = $pageArr["per_page"]; $has_pagination = $pageArr["has_pagination"];
::getOffset
返回分页计算出的偏移量
$offset = $paging->getOffset(); // return int value