1 回答
TA贡献1786条经验 获得超13个赞
一般分页代码流可能如下所示:
<?php
function getPageDisplayArray(int $currentPage, int $totalCount, int $pageLen = 10): array
{
$paginationArr[$currentPage] = 1;
$totalPagesToDisplay = floor(($totalCount) / $pageLen);
//Get preceding pages logic
$pagesBefore = $currentPage - 1;
$iBefore = 2; //Number of pages you want befpre the current page
while($pagesBefore > 0 && $iBefore > 0) {
$paginationArr[$pagesBefore--] = 0;
$iBefore--;
}
//Get anteceding pages
$pagesAfter = $totalPagesToDisplay - ($pagesBefore + 1);
$iAfter = min([$pagesAfter, 2]); //Number of pages you want after your current page
while(-$iAfter < 0) {
//Either return the two pages after or don't if there are no pages after
$paginationArr[$iAfter + $currentPage] = 0;
$iAfter--;
}
if($pagesAfter > 2) {
//Calcluate Midpoint
$midpoint = floor($totalPagesToDisplay / 2) + 1;
if(!isset($paginationArr[$midpoint])) {
$paginationArr[$midpoint] = 0;
}
}
//Link the first page always if it hasn't already been set
if(!isset($paginationArr[1])) {
$paginationArr[1] = 0;
}
//Link the final page if it has not already been set.
if(!isset($paginationArr[$totalPagesToDisplay])) {
$paginationArr[$totalPagesToDisplay] = 0;
}
ksort($paginationArr);
return $paginationArr;
}
其中当前页面、页面长度和总计数是您的参数。这里将在两侧显示两个页面作为显示页面,以及任何仲裁集的第一页、最后一页和中间页。这将返回一个配置数组,该数组将“活动”页面的值设置为 1,并将所有其他页面的键设置为页码,并将值设置为零。剩下的就是按照您认为合适的方式在布局中显示值。
- 1 回答
- 0 关注
- 74 浏览
添加回答
举报