Purpose of document

In Kohana 3, it was decided to remove the pagination method from the core. This was met with mixed and stong opinions on both sides.In my opinion, it was clunky and I never liked using it. In most cases, I ended up modifying it or writing something of my own. As I became familiar with other frameworks, I came across the Zend Framework paginator and came to like it. However, it was too heavy and I had to remember too may steps to get it to work right. I spent some time thinking about the properties I'd need and the methods I'd need the most eliminating the ones that I didn't want. Here is the fruits of that labor.

Usage:
Controller -
$posts = ORM::factory('table',$id);
$page = Arr::get($_GET, 'page',1);
$paginator = Pagination::factory($posts,10,$page);
View -
Parse the data in a foreach loop with $paginator->pageand display the controls with echo ($paginator->first != $paginator->last?$paginator->control(null,false):'')

In the Controller, collect the result set from the database. This can be either an object or an array. In my example, I'm using the ORM module to collect a record by an id. This is returned as on object and put into $posts.

Next, collect the current page that is being viewed. In my example, I'm using the $_GET param called 'page' and storing it in $page. By using the Arr::get() method, I can specify a default value (in this case '1') if the param is not found in the url.

Finally, I call the Paginator class passing the result, the number of items per page, and the current page number. These are stored in $paginator and passed to the view.

In the View, parse through the data for the current page by using a foreach loop and $paginator->page
For example, to display the data from the controller, use:
foreach ($paginator->page AS $page ){
echo $page->Log;
}

You call the paginator control passing the view name and a boolean to view the current page number.
echo ($paginator->first != $paginator->last?$paginator->control(null,false):'')

In my example displaying the controls, I'm using a binary conditional statement to check to see if that my first page is not my last page. If not, show the controls. The first parameter is the view in the module. NULLdefaults to "all" view. The second parameter is a boolean to display the page count. In "all", a true will display "page x of y" at the begining of the controller. You can create a different view my adding your own view file in the views folder in the pagination module.

Properties
  • public $current;//current page
  • public $last;//last page
  • public $next;//next page
  • public $pagecount;//number of pages
  • public $previous;//previous page
  • public $items = array();//all items in the result array
  • public $pages = array();//array_chunk of items
  • public $page = array();//items for current page
Methods
  • itemCountPerPage($count)// splits $items into chunks. puts the number of pages into pagecount and last
  • setCurrentPageNumber($pageNumber)// puts current page into $page and $current. calculates $next and $previous

Update: I've updated this module to now have 5 different types of paginators:

  • "all" - Display all of the avaiable pages from the result
  • "dropdown" - Displays a select list of available pages
  • "elastic" - Displays the previous five pages and the next five pages depending on the current page
  • "jump" - Displays groups of ten pages and individual pages if visiting the current group
  • "step" - Displays the next and previous links and not page links
The default view that the paginator uses is "all".

 

Please send me a note if you have any questions, comments, or suggestions to make this funciton better.

Download a zip file of this module here