Last active
April 26, 2016 17:00
-
-
Save shlomohass/9869e138a4fba0e7dc4c to your computer and use it in GitHub Desktop.
PHP - Quick and easy dynamic pagination function.
This file contains 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 | |
/** Paging function | |
* | |
* @param int $max -> results count | |
* @param int $inter -> display per page | |
* @param int $cur -> current page | |
* @param string $link -> base url | |
* @param string $urlquery -> url query variable name | |
* @param int $pad -> padding arround current page | |
* @return string -> UL | |
*/ | |
function paging($max, $inter, $cur, $link='', $urlquery='', $pad = 3) { | |
$pages = ceil(intval($max) / intval($inter)); | |
$res = array("<ul>"); | |
$cur--; | |
if ($pages > 8) { | |
for ($i=0; $i<$pages; $i++) { | |
if ($i === 0) { | |
$res[] = "<li ".(($i === $cur)?"class='active-page'":"")." data-page='".$i."'><a href='".$link.$urlquery."=".($i+1)."'>First</a></li>"; | |
} elseif ($i+1 == $pages) { | |
$res[] = "<li ".(($i === $cur)?"class='active-page'":"")." data-page='".$i."'><a href='".$link.$urlquery."=".($i+1)."'>Last</a></li>"; | |
} elseif($i > $cur - $pad && $i < $cur + $pad) { | |
$res[] = "<li ".(($i === $cur)?"class='active-page'":"")." data-page='".$i."'><a href='".$link.$urlquery."=".($i+1)."'>".($i+1)."</a></li>"; | |
} | |
} | |
} else { | |
for ($i=0; $i<$pages; $i++) { | |
$res[] = "<li ".(($i === $cur)?"class='active-page'":"")." data-page='".$i."'><a href='".$link.$urlquery."=".($i+1)."'>".($i+1)."</a></li>"; | |
} | |
} | |
$res[] = "</ul>"; | |
return implode('',$res); | |
} | |
/** Examples: */ | |
echo paging(27, 10, 1, "http://some.com/", "?page"); | |
/** | |
* <ul> | |
* <li data-page="0" class="active-page"><a href="http://some.com/?page=1">1</a></li> | |
* <li data-page="1"><a href="http://some.com/?page=2">2</a></li> | |
* <li data-page="2"><a href="http://some.com/?page=3">3</a></li> | |
* </ul> | |
*/ | |
echo paging(300, 25, 9, "http://some.com/", "?other=65&page"); | |
/** | |
* <ul> | |
* <li data-page="0"><a href="http://some.com/?other=65&page=1">First</a></li> | |
* <li data-page="6"><a href="http://some.com/?other=65&page=7">7</a></li> | |
* <li data-page="7"><a href="http://some.com/?other=65&page=8">8</a></li> | |
* <li data-page="8" class="active-page"><a href="http://some.com/?other=65&page=9">9</a></li> | |
* <li data-page="9"><a href="http://some.com/?other=65&page=10">10</a></li> | |
* <li data-page="10"><a href="http://some.com/?other=65&page=11">11</a></li> | |
* <li data-page="11"><a href="http://some.com/?other=65&page=12">Last</a></li> | |
* </ul> | |
* | |
*/ | |
echo paging(50, 5, 0, "http://some.com/", "?page"); | |
/** | |
* <ul> | |
* <li data-page="0"><a href="http://some.com/?page=1">First</a></li> | |
* <li data-page="2"><a href="http://some.com/?page=3">3</a></li> | |
* <li data-page="3"><a href="http://some.com/?page=4">4</a></li> | |
* <li class="active-page" data-page="4"><a href="http://some.com/?page=5">5</a></li> | |
* <li data-page="5"><a href="http://some.com/?page=6">6</a></li> | |
* <li data-page="6"><a href="http://some.com/?page=7">7</a></li> | |
* <li data-page="9"><a href="http://some.com/?page=10">Last</a></li> | |
* </ul> | |
* | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment