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 關注
- 95 瀏覽
添加回答
舉報