【问题标题】:How to integrate pagination in Kohana?如何在 Kohana 中集成分页?
【发布时间】:2013-09-20 13:07:27
【问题描述】:

我正在尝试在 kohana 中集成分页,但不知道如何集成它。以下是控制器功能

public function action_index() {
        //setup the model and view
        $_users = Model::factory('users');
        $us = $_users->get_users();

        $view = View::factory('users/index')->bind('user_list', $us);
        $this->template->set('content',$view);
    }

如何在此功能中添加分页? 我找到了一些分页代码,但无法集成。这是我找到的函数

 $this->pagination = new Pagination(array(
            'base_url'    => 'users/index/', 
            'uri_segment' => 'page',
            'total_items' => count($items->get_item_count())

请帮帮我

编辑: 我尝试了类似

public function action_index(){

    $query = DB::select()->from('user');
    // count number of users
    $total_users = count($query);;
    // set-up the pagination
    $pagination = Pagination::factory(array(
        'total_items' => $total_users,
        'items_per_page' => 10, // this will override the default set in your config
    ));
    // select rows using pagination's limit&offset 
    $users = $query->offset($pagination->offset)->limit($pagination->items_per_page)->execute();
    $view = View::factory('users/index')->bind('user_list', $users)->bind('pagination', $pagination);
    $this->template->set('content',$view);
}

现在没有发现错误,但没有显示分页。使用了 @DanielThompson 建议的 shadowhand 的分页模块

【问题讨论】:

标签: pagination kohana kohana-3


【解决方案1】:

我使用支持 Kohana 3+ 的shadowhand's pagination module,只要确保您获取与您的 Kohana 版本相同的分支,然后将其添加到您的模块目录。

更新您的application/bootstrap.php 文件:

Kohana::modules(array(
    // ...
    'pagination' => MODPATH.'pagination'
));

复制modules/pagination/config/pagination.phpapplication/config/pagination.php

在您的控制器操作中(例如用户):

// count number of users
$total_users = ORM::factory('User')->count_all();

// set-up the pagination
$pagination = Pagination::factory(array(
    'total_items' => $total_users,
    'items_per_page' => 10, // this will override the default set in your config
));

// get users using the pagination limit/offset
$users = ORM::factory('User')->offset($pagination->offset)->limit($pagination->items_per_page)->find_all();

// pass the users & pagination to the view
$this->view->bind('pagination', $pagination);
$this->view->bind('users', $users);

在你看来:

// loop over users
foreach($users as $user) {
    // ...
}

// display pagination view using
echo $pagination;

该模块有两个视图:基本视图或浮动视图,在配置文件中设置。您还可以为您的应用程序创建一个自定义的。

【讨论】:

  • 谢谢。再有几个疑问。我没有使用 ORM 方法。没有ORM可以吗?
  • 不熟悉ORM方法。对问题描述进行了一些更新,请您看一下。
  • @hariprasad 可能是因为您缺少执行:$query = DB::select()->from('user')->execute();
猜你喜欢
  • 1970-01-01
  • 2011-09-04
  • 2011-02-19
  • 1970-01-01
  • 1970-01-01
  • 2017-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多