Kohana 3 分页

发布于 2024-10-01 01:31:54 字数 57 浏览 4 评论 0原文

我真的不知道 Kohana 3 中的分页是如何工作的。Kohana 3 中是否有一个很好的分页示例?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

财迷小姐 2024-10-08 01:31:54
        // 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();

然后在视图中,您只需执行

echo $pagi; // ofc, after passing the Pagination object to view

这里发生的事情是 Pagination 类,使用它的 View 的 __toString() 魔术方法来渲染显示分页所需的 html。创建对象时可以修改所有分页参数(在我们的例子中将适当的键传递给传递给factory()方法的数组)。

分页的默认键是“page”(查询字符串),您也可以修改它。分页还有一个默认配置,您可以通过将其复制到 application/config 文件夹来覆盖它。

享受使用它的乐趣:)

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

小霸王臭丫头 2024-10-08 01:31:54

Kohana 3.1 中不包括分页。下载模块并将其放入modules文件夹中。在 application/bootstrap.php 中启用该模块。这是我的控制器页面。如需进一步配置,请将提供的配置文件从 modules/pagination/config/pagination.php 复制到 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();

您可能会收到错误 ErrorException [Notice]: Undefined属性:Request::$uri。在分页类(模块)中。为了修复它,

请使用 Request::current()->uri() 而不是 Request::current()->uri

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

戒ㄋ 2024-10-08 01:31:54

您可以在非官方 Kohana wiki 中找到一些不错的文档。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文