[英]PHP Pagination with x amount of pages
目前,我有一個運行正常的分頁腳本,盡管我缺少該功能。 目前,在$ pages列表中可能會顯示數百個頁面,因為例如[1] [...] 5、6、7 [..] [45]之間沒有過濾器可顯示。 。 這是我的代碼:
/** Pagination **/
$limit = 7;
$query = "SELECT COUNT(*) FROM users";
$result = $db->prepare($query);
$result->execute();
$pages_query = $result->fetchColumn(0);
$count = number_format($pages_query);
$pages = ceil($pages_query / $limit);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $limit;
ob_start();
echo("<span class='alignright'>");
if ($pages >= 1 && $page <= $pages){
if($page > 1){
$next = ($page - 1);
$link = "?page=$next";
echo("<a class='paginate' href='$link'><i class='icon-caret-left'></i></a>");
}
for ($x=1; $x<=$pages; $x++){
echo ($x == $page) ? "<strong style='font-weight: bold!important;'><a class='paginate' href='?page=$x'>$x</a></strong>" : "<a class='paginate' href='?page=$x'>$x</a>";
}
if($page < $pages){
$next = ($page + 1);
$link = "?page=$next";
echo("<a class='paginate' href='$link'><i class='icon-caret-right'></i></a>");
}
echo("</span>");
if($count > 0){
echo("<span class='smalltext'>Page <strong class='half'>$page</strong> of $pages:</span>");
} else {
echo("<span class='smalltext'>There are <span class='half'>$count</span> results to display.</span>");
}
$pagintion = ob_get_clean();
(它已經從其中的其他垃圾中刪除了,但這是一般的框架。)基本上,我正在嘗試找出如何限制它的出現,以使其在問題的頂部指定的底部位於“頁面之間”。 諸如此類:[<] [1] ... [4] [5] [6] ... [45] [>]如果這樣的話。
您可以在查詢中嘗試使用LIMIT
SELECT * FROM TABLE_NAME LIMIT STARTING_RESULT_NUMBER,RESULTS_PER_PAGE
RESULTS_PER_PAGE是您要顯示的每頁項目數STARTING_RESULT_NUMBER =(CURRENT_PAGE_NUMBER * RESULTS_PER_PAGE)
為了將頁面列表更改為[<] [1] ... [4] [5] [6] ... [45] [>]
而不是顯示所有頁碼,您可以將替換for
像這樣循環:
echo "<a class='paginate' href='?page=1'>1</a>";
if($page > 3) {echo "...";}
if($page > 2) {echo "<a class='paginate' href='?page=" . $x-1 . "'>" . $x-1 "</a>";}
if($page != 1 && $page != pages) {echo "<a class='paginate' href='?page=" . $x . "'>" . $x "</a>";}
if($page < $pages-1) {echo "<a class='paginate' href='?page=" . $x+1 . "'>" . $x+1 "</a>";}
if($page < $pages-2) {echo "...";}
if($pages >1) {echo "<a class='paginate' href='?page=1'>1</a>";}
Il將顯示第一頁,最后一頁,當前頁面以及之前和之后的頁面。
我個人對分頁的含義是,它必須可讀性強,以后再更改也很簡單。 我根據您的示例鍵入了以下代碼:
$totalPages = 145; //the total amount of pages
$selectedPage = 40; //the selected page
$pages = array(); //the array which is gonna hold the pages we need to display
$offset = 3; //the number of pages to select around the selected page
$closePages = range($selectedPage - $offset, $selectedPage + $offset); //select the pages that are in $offset of the selected page
array_filter($closePages, function($x) { //filter the pages below 1 and above $totalPages
return ($x <= $totalPages && $x >= 1 ? true : false );
});
array_push($pages, 1); //add the first page
array_push($pages, '...'); //add some dots
$pages = array_merge($pages, $closePages);
array_push($pages, '...'); //and again add some dots
array_push($pages, $totalPages); //add the last page
然后,使用foreach循環顯示頁面:
foreach($pages as $page) {
if (is_numeric($page)) {
if ($page != $selectedPage) $content .= ' <a href="?page=' . $page . '">' . $page . '</a> ';
else $content .= ' <a href="?page=' . $page . '"><strong>' . $page . '</strong></a> ';
} else
$content .= '[...]';
}
對這個答案發表評論后的一些解釋:
$ totalPages變量必須是頁面上的頁面總數(以您為例),而$ selectedPage是此時選擇的頁面。
$totalPages = ceil($pages_query / $limit);
$selectedPage = isset($_GET['page']) ? $_GET['page'] : 1;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.