Last active
January 2, 2021 12:20
-
-
Save w-mazed/8fea71be21d72ca2241e9ce838375fd0 to your computer and use it in GitHub Desktop.
PHP: Function to generate a limited number of pagination links
This file contains hidden or 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 | |
/** | |
* Function to generate a limited number of pagination links (First, Previous, Next and Last links included) | |
* | |
* @param integer Current page | |
* @param integer Items per page | |
* @param integer Items total | |
* @param integer Number of links to display | |
* | |
* @return string pagination links html | |
*/ | |
function pager($page, $offset, $total, $range = 5, $base_url = "") | |
{ | |
$pager = ""; | |
if ($offset < $total) { | |
$total_pages = ceil($total / $offset); | |
$pager .= '<nav aria-label="Page navigation example"> | |
<ul class="pagination">'; | |
// set current page to last page | |
if ($page > $total_pages) { | |
$page = $total_pages; | |
} // end if | |
// if current page is less than first page... | |
if ($page < 1) { | |
// set current page to first page | |
$page = 1; | |
} // end if | |
if ($page > 1) { | |
$first_page_title = "Go to first page"; | |
$first_page_text = "« Start"; | |
$pager .= "<li class=\"page-item\"> | |
<a href=\"{$base_url}?page=1\" class=\"page-link\" title=\"{$first_page_title}\" rel=\"first\"> | |
{$first_page_text} | |
</a> | |
</li>"; | |
// get previous page num | |
$prev_page = $page - 1; | |
$prev_page_title = "Go to preivous page"; | |
$prev_page_text = "‹ Previous"; | |
// $pager .= forward link for prevpage | |
$pager .= "<li class=\"page-item\"> | |
<a href=\"{$base_url}?page={$prev_page}\" class=\"page-link\" title=\"{$prev_page_title}\"> | |
{$prev_page_text} | |
</a> | |
</li>"; | |
} // end if | |
// loop to show links to range of pages around current page | |
for ($x = ($page - $range); $x < (($page + $range) + 1); $x++) { | |
// if it's a valid page number... | |
if (($x > 0) && ($x <= $total_pages)) { | |
// if we're on current page... | |
if ($x == $page) { | |
// 'highlight' it but don't make a link | |
$label = "Current page"; | |
$pager .= "<li class=\"page-item active\" aria-current=\"page\"> | |
<a href=\"{$base_url}?page={$x}\" class=\"page-link\" title=\"{$label}\"> | |
{$x} | |
</a> | |
</li>"; | |
// if not current page... | |
} else { | |
$title = "Go to page {$x}"; | |
// make it a link | |
$pager .= "<li class=\"page-item\"> | |
<a href=\"{$base_url}?page={$x}\" class=\"page-link\" title=\"{$title}\"> | |
{$x} | |
</a> | |
</li>"; | |
} // end else | |
} // end if | |
} // end for | |
// if not on last page, show forward and last page links | |
if ($page != $total_pages) { | |
// get next page | |
$next_page = $page + 1; | |
// $pager .= forward link for next page | |
$next_page_title = "Go to next page"; | |
$next_page_text = "Next ›"; | |
$pager .= "<li class=\"page-item\"> | |
<a href=\"?page={$next_page}\" class=\"page-link\" title=\"{$next_page_title}\" rel=\"next\"> | |
{$next_page_text} | |
</a> | |
</li>"; | |
$last_page_title = "Go to last page"; | |
$last_page_text = "Last »"; | |
// $pager .= forward link for lastpage | |
$pager .= "<li class=\"page-item\"> | |
<a href=\"?page={$total_pages}\" class=\"page-link\" title=\"{$last_page_title}\"> | |
{$last_page_text} | |
</a> | |
</li>"; | |
} // end if | |
$pager .= '</ul> | |
</nav>'; | |
} | |
return $pager; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment