Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
PHP Pagination - For Loops
//Before
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
}
}
//After
for ($i = max($page_number - 4, 1); $i < $page_number; $i++) {
$paginationCtrls .= '<a href="' . $_SERVER['PHP_SELF'] . '/page/' . $i . '">' . $i . '</a> &nbsp; ';
}
//Before
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
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> &nbsp; ';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment