Skip to content

Instantly share code, notes, and snippets.

@arthurgermano
Last active May 17, 2016 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurgermano/b612050b473dfd17076e8f63749d9c89 to your computer and use it in GitHub Desktop.
Save arthurgermano/b612050b473dfd17076e8f63749d9c89 to your computer and use it in GitHub Desktop.
Phalcon code
<?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;
}
}
@arthurgermano
Copy link
Author

arthurgermano commented May 17, 2016

To call this paginator you have to set your data on Phalcon Paginator and the call this helper like:

//IN CONTROLLER 
use Phalcon\Paginator\Adapter\Model as PaginatorModel;

$data = Model::find();
$currentPage = $this->request->getQuery('page', 'int');
$paginator = new PaginatorModel(array("data" => $data,"limit" => 20, "page" => $currentPage ));

$sizeOfInterval = 9;
$this->view->page = PaginatorHelper::setIntervalPages($paginator->getPaginate(),$sizeOfInterval);


//IN VIEW
if (isset($page->interval)) {
    echo '&nbsp;&nbsp;';
    foreach ($page->interval as $item) {
        if ($item != $page->current) {
            echo '<a href="' . $pageLink . '?page=' . $item . '">&LT;' . $item . '&GT;</a>';
        } else {
            echo '<a href="' . $pageLink . '?page=' . $item . '"><b>&LT;' . $item . '&GT;</b></a>';
        }
        echo '&nbsp;&nbsp;';
    }
}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment