Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create page numbers for pagination from current page number and total number of pages
<?php
// EXAMPLE USE IN COMMENTS
/**
* Convert current page number and the total number of pages into a number range for pagination.
* Use $edge_number_count to indicate how many numbers should appear to the left or right of the current page.
* If the start or end page is one off of the start or end of the entire set, the range is extended. This prevents page 2 from being the start page, and page N-1 from the end page.
*
* @param $current_page
* @param $num_pages
* @param int $edge_number_count
*
* @return array
*/
function rs_get_pagination_numbers( $current_page, $num_pages, $edge_number_count = 2 ) {
$start_number = $current_page - $edge_number_count;
$end_number = $current_page + $edge_number_count;
// Minus one so that we don't split the start number unnecessarily, eg: "1 ... 2 3" should start as "1 2 3"
if ( ($start_number - 1) < 1 ) {
$start_number = 1;
$end_number = min($num_pages, $start_number + ($edge_number_count*2));
}
// Add one so that we don't split the end number unnecessarily, eg: "8 9 ... 10" should stay as "8 9 10"
if ( ($end_number + 1) > $num_pages ) {
$end_number = $num_pages;
$start_number = max(1, $num_pages - ($edge_number_count*2));
}
if ( $end_number == $num_pages && (($edge_number_count*2) + 1) > ($end_number - $start_number) && $start_number > 1 ) {
$start_number = $end_number - ($edge_number_count*2);
}
return array( $start_number, $end_number );
}
@RadGH

This comment has been minimized.

Copy link
Owner Author

RadGH commented Jun 8, 2018

Example usage:

<?php
$current_page = 5; // your program should produce this
$num_pages = 10; // your program should produce this

$edge_number_count = 2; // configure this, how many numbers to show left/right of the current page

list( $start_number, $end_number ) = aa_ed_get_pagination_numbers( $current_page, $num_pages, $edge_number_count );

// Start pagination with "1 ..."
if ( $start_number > 1 ) {
	echo '<a href="?page=1" class="first">1</a>;
	echo '<span class="dots">&hellip;</span>';
}

for( $i = $start_number; $i <= $end_number; $i++ ) {
	if ( $i === $current_page ) {
		echo '<a href="?page='. $i .'" class="current">'. $i .'</a>;
	}else{
		echo '<a href="?page='. $i .'">'. $i .'</a>;
	}
}

// End pagination with "... 10"
if ( $end_number < $num_pages ) {
	echo '<span class="dots">&hellip;</span>';
	echo '<a href="?page='. $num_pages .'" class="last">'. $num_pages .'</a>;
}
@didousoft

This comment has been minimized.

Copy link

didousoft commented Sep 24, 2018

Hello @RadGH,
I've checked your code with following parameters :

$rows_count = 2000;
$rows_per_page = 2;
$current_page = 1;
var_dump(rs_get_pagination_numbers($current_page,$rows_count,$rows_per_page));exit;

And i get always the same response even when i change the current_page value.
Result is :

array (size=2)
  0 => int 1
  1 => int 5

Best regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.