Skip to content

Instantly share code, notes, and snippets.

@wakasann
Created June 8, 2018 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wakasann/374a05a4c913ec5f3753ba7fcd97f2e6 to your computer and use it in GitHub Desktop.
Save wakasann/374a05a4c913ec5f3753ba7fcd97f2e6 to your computer and use it in GitHub Desktop.
array pagination
<?php
function p($array){
echo "<pre>";
print_r($array);
echo "</pre>";
}
$array = array();
for($i = 0;$i < 5;$i++){
array_push($array,$i);
}
$array_length = count($array);
$per_page = 20;
$page = (int) ceil($array_length/$per_page);
echo $page;
for($i = 0;$i < $page;$i++){
$start_index = $i * $per_page;
$current_count = $per_page * ($i+1);
$offset = ($array_length >= $current_count)?20:$array_length - ($per_page * $i);
$data = array_slice($array,$start_index,$offset);
p($data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment