Skip to content

Instantly share code, notes, and snippets.

@shiny
Created February 28, 2017 09:33
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 shiny/263233a1ee954e717eeac81e60520618 to your computer and use it in GitHub Desktop.
Save shiny/263233a1ee954e717eeac81e60520618 to your computer and use it in GitHub Desktop.
page.php
<?php
class Page
{
public static function Generate($page, $count, $perpage=50)
{
$max = ceil($count / $perpage);
if($page > $max) {
$page = $max;
} else if($page < 1) {
$page = 1;
}
$pages = range(1, $max);
$result = [];
foreach ($pages as $p) {
if($max < 7) {
$result[] = $p;
} else if($page - 7 > $max) {
$result[] = $p;
} else if($p==1) {
$result[] = 1;
} else if( $p > ($page-3) && $p < ($page+3)) {
$result[] = $p;
} else if($result[count($result)-1]!='..') {
$result[] = '..';
}
}
return $result;
}
}
$page = filter_input(INPUT_GET, 'page');
$pages = Page::Generate($page, 4000);
?>
<nav>
<?php foreach($pages as $p): ?>
<?php if($p=='..'): ?>
<span>..</span>
<?php else: ?>
<a href="?page=<?=$p?>"><?=$p?></a>
<?php endif; ?>
<?php endforeach; ?>
</nav>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment