Last active
August 29, 2015 14:22
-
-
Save joeykrim/ba58ae2f82d586523786 to your computer and use it in GitHub Desktop.
PHP Pagination - For Loops
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
//Before | |
for($i = $pagenum-4; $i < $pagenum; $i++){ | |
if($i > 0){ | |
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> '; | |
} | |
} | |
//After | |
for ($i = max($page_number - 4, 1); $i < $page_number; $i++) { | |
$paginationCtrls .= '<a href="' . $_SERVER['PHP_SELF'] . '/page/' . $i . '">' . $i . '</a> '; | |
} | |
//Before | |
for($i = $pagenum+1; $i <= $last; $i++){ | |
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> '; | |
if($i >= $pagenum+4){ | |
break; | |
} | |
} | |
//After | |
for ($i = $page_number + 1; $i <= min($last_page, $page_number + 4); $i++) { | |
$paginationCtrls .= '<a href="' . $_SERVER['PHP_SELF'] . '/page/' . $i . '">' . $i . '</a> '; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment