I’ve been doing some pagination recently and used the following:
if ( $totalPages > $pagesToShow ) {
$start = $pageNumber - floor($pagesToShow/2);
$end = $pageNumber + floor($pagesToShow/2);
while ( $start < 1 ) {
$start++;
$end++;
}
while ( $end > $totalPages ) {
$start--;
$end--;
}
} else {
$start = 1;
$end = $totalPages;
}
to work out where to start and end the list of surrounding pages. So that a paging list can be created like << < 1 2 3 4 5 > >>’.
Just wondering if there is a better method as using loops like that seems a little odd.
You can replace the first loop
with
Something similar can be done for the second loop, but the other way around:
Edit: It is much easier to just trim the page numbers which are out of bounds, replace your example code with:
$pagesToShowis the maximum amount of pages to show before and after the current page (if not out of bounds)