I'm really lost on how pagination works in kohana 3. Is there a good example of pagination in Kohana 3 anywhere?
3 Answers
// Get the total count of articles
$count = $this
->_profil
->articles
->count_all();
// Create the pagination object
$pagi = Pagination::factory(array(
'items_per_page' => 4,
'total_items' => $count,
));
// Find actual articles
$articles = $this->_profil
->articles
->join_categories()
->order_by('id','DESC')
->limit($pagi->items_per_page)
->offset($pagi->offset)
->find_all();
and then in the View, you just do
echo $pagi; // ofc, after passing the Pagination object to view
What happens here is Pagination class using it's View's __toString() magic method to render html needed to display pagination. All pagination params can be modified when creating the object (passing appropriate keys to the array passed to factory() method in our case).
Default key for pagination is "page" (query string), while you can modify that as well. Pagination also has a default config, which you can override by copying it to application/config folder.
Enjoy using it :)

- 6,942
- 3
- 32
- 39
-
1also ORM has useful `count_last_query()` method for counting result rows. – biakaveron Nov 05 '10 at 19:40
-
1though I wouldn't rely on that method too much when having a complicated query :) This way we can reuse the count query as well, by adding reset(FALSE) before the counting occurs. – Kemo Nov 05 '10 at 21:28
-
The `reset(FALSE)` method worked really nice, I think it saved my afternoon, heh. – Alejandro García Iglesias Jun 07 '11 at 21:55
-
Don't forget echo $pagi->render() or you will get a to string error in kohana 3.1... Just noting for future searches since I just used this answer to help my ko 3.1 problem. Thanks – Kai Qing Nov 18 '11 at 21:51
-
@KaiQing __toString() calls the render() method anyways, though exceptions inside of it can't be caught – Kemo Nov 22 '11 at 15:13
In Kohana 3.1 pagination is not included. Download the module and put it in the modules folder. Enable the module in your application/bootstrap.php .This is my controller page. For further configuration copy the provided config file from modules/pagination/config/pagination.php to application/config/pagination.php
$per_page =2;
$page_num = $this->request->param('page', 1);
$offset = ($page_num - 1) * $per_page;
$view =View::factory('image/imagelist')->bind('page_links',$page_links)->bind('results', $results)->bind('pagination', $pagination);
// Get the total count of records in the database
$userid = Auth::instance()->get_user()->pk();
$count=ORM::factory('user_image')->where('app_userid','=',$userid)->count_all();
// Create an instance of Pagination class and set values
$pagination = Pagination::factory(array(
'total_items' => $count,
'current_page' => array('source' => 'image/imagelist', 'key' => 'page'),
'items_per_page' => $per_page,
'offset' => $offset,
'view' => 'pagination/basic'
));
// Load specific results for current page
$results = DB::select()->from('user_images')
->where('app_userid','=',$userid)
->order_by('image_id','ASC')
->limit($pagination->items_per_page)
->offset($pagination->offset)->execute();
$page_links = $pagination;
$this->template->content=$view->render();
You may get error ErrorException [ Notice ]: Undefined property: Request::$uri
. in the pagination class (module). In order to fix fix it
Use Request::current()->uri()
instead of Request::current()->uri

- 1,796
- 22
- 27