Kohana 3 pagination

AdamB picture AdamB · Nov 4, 2010 · Viewed 7.1k times · Source

I'm really lost on how pagination works in kohana 3. Is there a good example of pagination in Kohana 3 anywhere?

Answer

Kemo picture Kemo · Nov 4, 2010
        // 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 :)