Skip to content

Instantly share code, notes, and snippets.

@munsiwoo
Last active February 14, 2018 03:53
Show Gist options
  • Save munsiwoo/582fed3c2ea7de647a9c96ce62e318a2 to your computer and use it in GitHub Desktop.
Save munsiwoo/582fed3c2ea7de647a9c96ce62e318a2 to your computer and use it in GitHub Desktop.
pagination
<?php
function pagination($page, $limit, $count) {
echo "<style>a { text-decoration:none; color:black; }</style>";
for($x = ($page-1); ($x%$limit) != 0; --$x);
if(($page-1) >= $limit) {
echo "<a href=\"?page={$x}\">&lt;Prev</a> ";
}
$limit = (($x+$limit) > $count) ? $count : $x+$limit;
for($p = ($x+1); $p <= $limit; ++$p) {
if($p == $page) echo "<b><a href=\"?page={$p}\">{$p}</a></b> ";
else echo "<a href=\"?page={$p}\">{$p}</a> ";
}
if($p <= $count) {
echo "<a href=\"?page={$p}\">Next&gt;</a>";
}
}
if(!isset($_GET['page']) || $_GET['page'] < 1) {
header('Location: ?page=1');
}
$page = (int)$_GET['page']; // select page
$limit = 5; // divide
$count = 23; // total number of pages
echo '<pre>';
echo "total page : {$count}".PHP_EOL;
pagination($page, $limit, $count);
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment