Created
August 23, 2018 18:37
-
-
Save wallacemaxters/3149260f58e6a66daeccc0f730db03b1 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* @param int $current Página atual | |
* @param int $total Total da Paginação | |
* @param int $limit Limite de links exibidos | |
* @param int $start_at Valor inicial. Geralmente o Padrão é 1 | |
* @return \Generator | |
*/ | |
function range_limit($current, $total, $limit = 10, $start_at = 1) | |
{ | |
$middle = ceil($limit / 2); | |
$current = max($start_at, min($total, $current)); | |
$start = $current - $middle; | |
$end = $middle + $current; | |
if ($start <= $start_at) { | |
$start = $start_at; | |
$end = $limit; | |
} elseif ($end >= $total) { | |
$end = $total; | |
$start = $total - $limit; | |
} | |
for ($i = $start; $i <= $end; $i++) yield $i; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
Result: