Created
June 8, 2018 03:20
-
-
Save wakasann/374a05a4c913ec5f3753ba7fcd97f2e6 to your computer and use it in GitHub Desktop.
array pagination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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