Skip to content

Instantly share code, notes, and snippets.

@SeanJA
Forked from gavinblair/pagination_view.php
Created June 8, 2010 11:27
Show Gist options
  • Save SeanJA/429886 to your computer and use it in GitHub Desktop.
Save SeanJA/429886 to your computer and use it in GitHub Desktop.
<?php
//this would come from a database count function
$totalPages = 200;
//presumably this comes from the get variable
$currentPage = 20;
//if the current page > total pages, just set it to the number of pages,
//someone is messing with the url
if ($currentPage > $totalPages) {
$currentPage = $totalPages;
}
// The goal is to have a pager showing (if available) 5 numbers,
// with the current page number in the middle, if available.
// <<First and <Previous are shown if you can't see page 1,
// and Next> and Last>> are shown if you can't see the last page.
$pager = '<ul class="pager">' . PHP_EOL;
$start = 1;
if ($currentPage > 2) {
$start = $currentPage - 2;
}
//we can start by assuming that the page is start + 4 because we want to show 5
$end = $start + 4;
if ($end > $totalPages) {
$end = $totalPages;
} else if ($end == $totalPages && $start > 2) {
$diff = $totalPages - $currentPage;
$start -= ( 2 - $diff);
} else if ($start == 1 && $end < $totalPages) {
$diff = $currentPage - 1;
$end += ( 2 - $diff);
}
if ($start > 1) {
$pager .= '<li><a href="/1">&laquo; First</a></li>' . PHP_EOL;
$prev = $currentPage - 1;
$pager .= '<li><a href="/' . $prev . '">&lsaquo; Previous</a></li>' . PHP_EOL;
$pager .= '<li>...</li>' . PHP_EOL;
}
for ($i = $start; $i<=$end; $i++) {
$pager .= '<li><a ';
if ($currentPage == $i) {
$pager .= 'class="current" ';
}
$pager .= 'href="/' . $i . '">' . $i . '</a></li>' . PHP_EOL;
}
if ($end < $totalPages) {
$pager .= '<li>...</li>' . PHP_EOL;
$pager .= '<li><a href="/' . ++$currentPage . '">Next &rsaquo;</a></li>' . PHP_EOL;
$pager .= '<li><a href=/"' . $totalPages . '">Last &raquo;</a></li>' . PHP_EOL;
}
$pager .= '</ul>';
echo $pager;
@gavinblair
Copy link

I've never seen PHP_EOL before. http://coffeepaste.tagabukid.com/2009/08/phpeol-php-end-of-line.html says it's the best cross-compatible way to output a linebreak. Nice.

@SeanJA
Copy link
Author

SeanJA commented Jun 8, 2010

That is why I use it (also it auto completes so no chance of a typo), and it doesn't go in quotes, so no need to switch quoting styles all over the place.

@SeanJA
Copy link
Author

SeanJA commented Jun 8, 2010

Also there is DIRECTORY_SEPARATOR (which I usually define as DS since I end up using it a lot... and PATH_SEPARATOR which is important if you are adding things to the path (unix and windows can have different path separators).

@SeanJA
Copy link
Author

SeanJA commented Jun 8, 2010

PHP_EOL isn't really needed here, I added it so I would not have to scroll to the right to see if I finally got my solution correct (since I was printing it out on the command line).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment