Вопрос

I am trying to integrate pagination in kohana, but don't know how to integrate it. Following is the controller function

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);
    }

How can i add pagination in this function? I found some code for pagination but couldn't integrate it. This is the function i found

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

Please help me

EDIT: I tried something like

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);
}

Now no error found but pagination not showing up. Used shadowhand's pagination module suggested by @DanielThompson

Это было полезно?

Решение

I use shadowhand's pagination module which supports Kohana 3+, just make sure you grab the same branch as your Kohana version, then add it to your modules directory.

Update your application/bootstrap.php file:

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

Copy modules/pagination/config/pagination.php to application/config/pagination.php

In your controller action (e.g. users):

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

In your view:

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

// display pagination view using
echo $pagination;

The module comes with two views: basic or floating which is set in the config file. You could also create a custom one for your application.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top