Last active
May 17, 2016 18:45
-
-
Save arthurgermano/b612050b473dfd17076e8f63749d9c89 to your computer and use it in GitHub Desktop.
Phalcon code
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 | |
namespace App\Helpers; | |
class PaginatorHelper { | |
/* | |
* Params | |
* $paginator = PHALCON PAGINATOR - must already be loaded with information | |
* $range = range of pages that must appear to user | |
* | |
* Returns | |
* an array with the Integer number of pages | |
*/ | |
public static function setIntervalPages($paginator, $range = 5) { | |
//The minimum size is 3 | |
if($range <= 2){ | |
$range = 3; | |
} | |
//The range should never exceed the total_pages | |
if($range > $paginator->total_pages){ | |
$range = $paginator->total_pages; | |
} | |
//divinding range to show half before current and half after current | |
$start = $paginator->current - intval($range/2); | |
$end = $paginator->current + intval($range/2); | |
//if negative means that current is around start position | |
if($start <= 1){ | |
$start = 1; | |
$end = $range; | |
//if greater then total pages means is around final position | |
}else if($end > $paginator->total_pages){ | |
$start = $paginator->total_pages - $range +1; | |
$end = $paginator->total_pages; | |
} | |
//starting by 1 because pagination starts with 1 | |
$i = 1; | |
//There were some issues with interval not existing, so count is needed | |
//to insure no warnings will be shown | |
$count = 1; | |
$inverval = array(); | |
//while will pass to all pages in disposal | |
while($i <= $paginator->total_pages){ | |
//if reaches start position and minor than end position | |
//start interval of values to show | |
if($i >= $start && $i <= $end && $count <= $range){ | |
$interval[] = $i; | |
$count++; | |
} | |
$i++; | |
} | |
//setting interval to PHALCON PAGINATOR | |
$paginator->interval = $interval; | |
return $paginator; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To call this paginator you have to set your data on Phalcon Paginator and the call this helper like: