ck / pagecalc
计算分页结果中的页面编号和游标
dev-master
2019-08-27 13:59 UTC
Requires
- php: >=5.0
Requires (Dev)
- phpunit/phpunit: ^5.7 | ^6
This package is auto-updated.
Last update: 2024-08-28 01:33:08 UTC
README
PageCalc
它计算页面编号和游标(当前页的第一项编号)的反向。
它还计算
- 下一个游标(如果适用,则下一页的第一项编号)
- 上一个游标(如果适用,则上一页的第一项编号)
- 最后一页的游标(最后一页的第一项编号)
- 如果适用,则下一个页面编号
- 如果适用,则上一个页面编号
- 最后一页编号
- 总页数
安装
使用composer安装
composer require "ck/pagecalc:@dev"
使用示例
require '../vendor/autoload.php'; use CK\PageCalc; $pc = new PageCalc(20) ; // Initiate with a default number of items on one page (limit) // Someone makes a request on page 3 and a limit of 20 items on the page $pc->gotoPage($_GET['page'], $_GET['limit']); // Now do your query using the cursor (offset) doYourQuery($query = $_GET['q'], $offset = $pc->getCursor(), $limit = $_GET['limit']); /* * With the results you have a total number of results (e.g. $total = 100) */ echo 'There are '. $pc->getTotalPages($total) .' pages'; # There are 5 pages echo 'Page '. $pc->getPage() . ' starts at ' .$pc->getCursor(); # Page 3 starts at 41 // make sure there is a next page if($pc->getNextPage($total)) { echo 'Next page is '. $pc->getNextPage($total) . ' and starts at ' . $pc->getNextCursor($total); # Next page is 4 and starts at 61 } // make sure there is a previous page if($pc->getPreviousPage()) { echo 'Previous page was '. $pc->getPreviousPage() . ' and started at ' . $pc->getPreviousCursor(); # Previous page was 2 and started at 21 } // there is always a last page (same as total number of pages) echo $pc->getLastPage($total) .' is the last page and starts at '. $pc->getLastCursor($total); # 5 is the last page and starts at 81
方法
__construct($limit)
设置默认限制。如果限制小于1,则默认限制为1。
moveCursor($cursor, $limit = false)
移动游标,计算页面编号并可选地设置新的限制
由于游标始终是当前页的第一项编号,因此游标必须是限制的倍数加1。如果游标编号不正确,方法moveCursor将更正它。例如。
$pc = new PageCalc(3); // limit 3 $pc->moveCursor(5); // cursor can't be 5 echo $pc->getCursor(); // current cursor is now 4. //4 is the first item number on page 2, wich contains item number 5
gotoPage($page, $limit = false)
设置当前页码,计算游标编号并可选地设置新的限制
getCursor()
获取当前页第一项的编号
getNextCursor($total)
$total是项目总数。
获取下一页第一项的编号。如果没有下一页,则返回false。
getPreviousCursor()
获取上一页第一项的编号。如果没有上一页,则返回false。
getLastCursor($total)
$total是项目总数。
获取最后一页第一项的编号。
getPage()
获取当前页码
getNextPage($total)
$total是项目总数。
获取下一页的页码。如果没有下一页,则返回false。
getPreviousPage()
获取上一页的页码。如果没有上一页,则返回false。
getLastPage($total)
$total是项目总数。
获取最后一页的页码。
getTotalPages($total)
$total是项目总数。
获取总页数。
getNumberOfItems($total)
$total是项目总数。
获取当前页的项目数量。