$page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
$total = count( $yourDataArray ); //total items in array
$limit = 20; //per page
$totalPages = ceil( $total/ $limit ); //calculate total pages
$page = max($page, 1); //get 1 page when $_GET['page'] <= 0
$page = min($page, $totalPages); //get last page when $_GET['page'] > $totalPages
$offset = ($page - 1) * $limit;
if( $offset < 0 ) $offset = 0;
$yourDataArray = array_slice( $yourDataArray, $offset, $limit);
print_r($yourDataArray);
Last active
August 12, 2021 22:06
-
-
Save mknparreira/a6a0ada535d7cada6546f3bf3c82b44b to your computer and use it in GitHub Desktop.
PHP | Emulate an pagination using arrays
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reference: https://stackoverflow.com/questions/26451362/how-to-add-php-pagination-in-arrays/31989815