Kohana 3 pagination
Asked Answered
F

3

6

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

Forfeiture answered 4/11, 2010 at 19:56 Comment(0)
S
14
        // 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 :)

Shorthand answered 4/11, 2010 at 22:42 Comment(5)
also ORM has useful count_last_query() method for counting result rows.Deadlight
though 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.Shorthand
The reset(FALSE) method worked really nice, I think it saved my afternoon, heh.Hagberry
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. ThanksPhototherapy
@KaiQing __toString() calls the render() method anyways, though exceptions inside of it can't be caughtShorthand
S
3

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

Seigel answered 26/8, 2011 at 8:21 Comment(0)
G
2

You can find some decent docs in the unofficial Kohana wiki.

Gas answered 4/11, 2010 at 19:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.