开发者

Kohana 3 pagination

I'm really lost on how pagination works in kohan开发者_运维知识库a 3. Is there a good example of pagination in Kohana 3 anywhere?


        // 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 :)


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜