Skip to content

Instantly share code, notes, and snippets.

@robmint
Last active October 29, 2019 11:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save robmint/e4b037aba1687195524c to your computer and use it in GitHub Desktop.
Save robmint/e4b037aba1687195524c to your computer and use it in GitHub Desktop.
Pagination like flickr, digg, github - the standard pagination pattern
<?php
// based on the work of Jason Coleman
// http://www.strangerstudios.com/blog/2006/07/paginate-your-site-like-digg/
/* The pagination function simply spits out a data structure
that you can render any way you like.
See also the javascript version for node:
https://gist.github.com/robmint/4651cf1c6f336e663dba
*/
$p = getPaginationString(2,150,15,1,"/","p=");
// simple text renderer
$out = '';
foreach($p as $v) {
if($v['previous'])
$out .= " < ";
elseif ($v['next'])
$out .= " > ";
elseif ($v['ellipsis'])
$out .= " ... ";
elseif ($v['selected'])
$out.= " [".$v['label']."] ";
else {
$out.= " ".$v['label']." ";
}
}
// show data structure
print_r($p);
// show rendered output
print $out . "\n";
function getPaginationString($page = 1, $totalitems, $limit = 15, $adjacents = 1, $targetpage = "/", $pagestring = "?page=")
{
//defaults
if(!$adjacents) $adjacents = 1;
if(!$limit) $limit = 15;
if(!$page) $page = 1;
if(!$targetpage) $targetpage = "/";
//other vars
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($totalitems / $limit); //lastpage is = total items / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
$pages = array();
if($lastpage > 1)
{
//previous button
if ($page > 1)
$pages[] = array('previous' => true, 'url' => $targetpage . $pagestring . $prev);
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pages[] = array('selected' => true, 'label' => $counter);
else
$pages[] = array('label' => $counter, 'url' => $targetpage . $pagestring . $counter);
}
}
elseif($lastpage >= 7 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 3))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pages[] = array('selected' => true, 'label' => $counter);
else
$pages[] = array('label' => $counter, 'url' => $targetpage . $pagestring . $counter);
}
$pages[] = array('ellipsis' => true);
$pages[] = array('label' => $lpm1, 'url' => $targetpage . $pagestring . $lpm1);
$pages[] = array('label' => $lastpage, 'url' => $targetpage . $pagestring . $lastpage);
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pages[] = array('label' => '1', 'url' => $targetpage . $pagestring . "1");
$pages[] = array('label' => '2', 'url' => $targetpage . $pagestring . "2");
$pages[] = array('ellipsis' => true);
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pages[] = array('selected' => true, 'label' => $counter);
else
$pages[] = array('label' => $counter, 'url' => $targetpage . $pagestring . $counter);
}
$pages[] = array('ellipsis' => true);
$pages[] = array('label' => $lpm1, 'url' => $targetpage . $pagestring . $lpm1);
$pages[] = array('label' => $lastpage, 'url' => $targetpage . $pagestring . $lastpage);
}
//close to end; only hide early pages
else
{
$pages[] = array('label' => '1', 'url' => $targetpage . $pagestring . "1");
$pages[] = array('label' => '2', 'url' => $targetpage . $pagestring . "2");
$pages[] = array('ellipsis' => true);
for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pages[] = array('selected' => true, 'label' => $counter);
else
$pages[] = array('label' => $counter, 'url' => $targetpage . $pagestring . $counter);
}
}
}
//next button
if ($page < $counter - 1)
$pages[] = array('next' => true, 'url' => $targetpage . $pagestring . $next);
}
return $pages;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment