Kohana 3 pagination

2020-06-04 04:33发布

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

3条回答
我只想做你的唯一
2楼-- · 2020-06-04 05:17

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

查看更多
叼着烟拽天下
3楼-- · 2020-06-04 05:20
        // 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 :)

查看更多
等我变得足够好
4楼-- · 2020-06-04 05:23

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

查看更多
登录 后发表回答