Last active
May 10, 2022 19:09
-
-
Save sean3z/63c29195854b0f9e61cad62c27e36239 to your computer and use it in GitHub Desktop.
Pagination example
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 | |
function handle_pagination($total, $page, $shown, $url) { | |
$pages = ceil( $total / $shown ); | |
$range_start = ( ($page >= 5) ? ($page - 3) : 1 ); | |
$range_end = ( (($page + 5) > $pages ) ? $pages : ($page + 5) ); | |
if ( $page >= 1 ) { | |
$r[] = '<span><a href="'. $url .'">« first</a></span>'; | |
$r[] = '<span><a href="'. $url . ( $page - 1 ) .'">‹ previous</a></span>'; | |
$r[] = ( ($range_start > 1) ? ' ... ' : '' ); | |
} | |
if ( $range_end > 1 ) { | |
foreach(range($range_start, $range_end) as $key => $value) { | |
if ( $value == ($page + 1) ) $r[] = '<span>'. $value .'</span>'; | |
else $r[] = '<span><a href="'. $url . ($value - 1) .'">'. $value .'</a></span>'; | |
} | |
} | |
if ( ( $page + 1 ) < $pages ) { | |
$r[] = ( ($range_end < $pages) ? ' ... ' : '' ); | |
$r[] = '<span><a href="'. $url . ( $page + 1 ) .'">next ›</a></span>'; | |
$r[] = '<span><a href="'. $url . ( $pages - 1 ) .'">last »</a></span>'; | |
} | |
return ( (isset($r)) ? '<div>'. implode("\r\n", $r) .'</div>' : ''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment