Created
February 28, 2017 09:33
-
-
Save shiny/263233a1ee954e717eeac81e60520618 to your computer and use it in GitHub Desktop.
page.php
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 | |
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