Last active
November 28, 2017 05:41
-
-
Save JoelLisenby/1cb9d6c0781fe7e9d325e4cb18e47b9c to your computer and use it in GitHub Desktop.
A super clean 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 | |
/* pagination($limit, $page, $total) | |
Designed for clean urls with a /slug/page/2 format | |
*/ | |
function pagination($current_url, $limit = 10, $page = 1, $total = 100) { | |
if($total > $limit && $page !== 'all') { | |
// pagination | |
$prevpage = ($page - 1 > 0 && $page > 2 ? '/page/'. ($page - 1) : ''); | |
$prev = preg_replace('/\/page\/[0-9]*/', $prevpage, $current_url); | |
$nextpage = ($page * $limit < $total ? '/page/'. ($page + 1) : ''); | |
$next = ($page > 1 ? preg_replace('/\/page\/[0-9]*/', $nextpage, $current_url) : $current_url . $nextpage); | |
$all = preg_replace('/\/page\/[0-9]*/', '', $current_url). '/page/all'; | |
echo '<div class="pagination">'; | |
echo ($page - 1 > 0 ? '<a class="prev" href="'.$prev.'">Prev</a>' : ''); | |
echo '<span>'. $page .' of '. ceil($total / $limit) .'</span>'; | |
echo ($page * $limit < $total ? '<a class="next" href="'.$next.'">Next</a>' : ''); | |
echo ($page !== 'all' ? '<a href="'. $all .'">View All</a>' : ''); | |
echo '</div><!-- .pagination -->'; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment