For the sake of the screenshot, I made the background light grey, this is not included in the default styling.
See below for all the functions documentation.
/**
* @title Typos pagiLinks() Pagination Function (www.typo-it.com).
* @param (int) $curPage - This is the page that is currently loaded.
* @param (int) $totalResults - The total amount of results.
* @param (int) $maxResults - The maximum amount of results per page.
* @param (mixed) $urlQuery - Any URL Queries that need to be appended to the pagination links.
* Set to false for no queries or leave it out (ommit it) to use all existing queries.
* @return (string) A series of pagination links.
*
* Syntax Example Using All Existing Url Queries:
* echo pagiLinks(7, 230, 10);
* Syntax Example Using Your Own Url Queries:
* echo pagiLinks(7, 230, 10, 'sky=blue&snow=cold');
* Syntax Example Using No Additional Url Queries:
* echo pagiLinks(7, 230, 10, false);
*
* Returned links example:
* Previous Page 1 ... 4 5 6 '7' 8 9 10 ... 23 Next Page
*
* Notes(1):
* The current page is styled to be bold and slightly larger using the provided the css, it
* won't actually have quotes like above.
* Notes(2):
* The url query string ($urlQuery) can look like 'sky=blue&snow=cold' or '&sky=blue&snow=cold'
* or '?sky=blue&snow=cold'. If you do not include a URL query, the function will assume it
* should pull the queries using $_SERVER, if you set $urlQuery to false, no additional url
* queries will be appended.
* Notes(3):
* The included styling will spread accross the whole div you echo pagiLinks() in. It will split
* it into thirds, in the first 1/3 will be the Previous Page link (when applicable), then, in the
* second 1/3, always centered is the numbered links and finally is the Next Page link
* (if applicable) in the third 1/3.
* Notes(4):
* The current page ($curPage) can be accessed the normal way using $_GET['curpage']. I decided
* not to have the script grab this automatically for the sake of versatility.
*
* Default CSS:
* .pagination { padding:0px 5px; width:100%; }
* .pagiprevious { width:33%; text-align:left; float:left; }
* .pagipages { text-align:center; }
* .paginext { width:33%; text-align:right; float:right; }
* .pagimiddle a { margin:0px 5px }
* .pagifirst a { margin:0px 5px 0px 0px }
* .pagilast a { margin:0px 0px 0px 5px }
* .pagiselected { font-weight:bolder; font-size:110%; color:#505050; }
*/
function pagiLinks($curPage, $totalResults, $maxResults, $urlQuery = true) {
$urlQuery = ($urlQuery === true) ? $_SERVER['QUERY_STRING'] : $urlQuery;
if (!empty($urlQuery)) {
$urlQuery = (strpos($urlQuery, '?') === 0) ? substr($urlQuery, 1) : $urlQuery;
parse_str($urlQuery,$urlQuery);
$urlQuery = '&' . http_build_query(array_diff_key($urlQuery, array('curpage' => '')));
}
if ($totalResults > $maxResults) {
$pagiLink = '<div class="pagination"><span class="pagiprevious">';
$totalPages = ceil($totalResults / $maxResults);
$pagiLink .= ($curPage - 1 > 0) ? "<a href=\"" . $_SERVER['PHP_SELF'] . '?curpage=' . ($curPage - 1) . $urlQuery . '">Previous Page</a></span>' : ' </span>';
$pagiLink .= ($curPage + 1 <= $totalPages) ? "<span class=\"paginext\"><a href=\"" . $_SERVER['PHP_SELF'] . '?curpage=' . ($curPage + 1) . $urlQuery . '">Next Page</a></span>' : '<span class="paginext"> </span>';
$selected = ($curPage == 1) ? 'class="pagiselected"' : '';
$pagiLink .= "<span class=\"pagipages\"><span class=\"pagifirst\"><a $selected href=\"" . $_SERVER['PHP_SELF'] . "?curpage=1$urlQuery \">1</a>...</span><span class=\"pagimiddle\">";
$i = ($curPage - 3 >= 2) ? $curPage - 3 : 2;
while ($i < $totalPages) {
$selected = ($curPage == $i) ? 'class="pagiselected"' : '';
if ($i <= $curPage + 3) { $pagiLink .= "<a $selected href=\"" . $_SERVER['PHP_SELF'] . "?curpage=$i" . $urlQuery . " \">$i</a>"; }
$i = $i + 1;
}
$selected = ($curPage == $totalPages) ? 'class="pagiselected"' : '';
$pagiLink .= "</span><span class=\"pagilast\">...<a $selected href=\"" . $_SERVER['PHP_SELF'] . "?curpage=$totalPages" . $urlQuery . " \">$totalPages</a></span></span></div>";
return "$pagiLink";
}
}