1.ติดตั้ง Module Pagination
- ดาน์วโหลด https://github.com/kloopko/kohana-pagination (Pagination for Kohana 3.2
- แตกไฟล์นำไปไว้ที่ http://www.yoursite.com/modules/pagination
2.เข้าไปแก้ไข application/bootstrap.php
Kohana::modules(array( 'userguide' => MODPATH.'userguide', 'database' => MODPATH.'database', // Database access 'pagination' => MODPATH.'pagination', ));3.ทำการสร้างไฟล์ Config (application/config/pagination.php)
<?php defined('SYSPATH') or die('No direct script access.');
return array(
// Application defaults
'default' => array(
'current_page' => array('source' => 'query_string', 'key' => 'page'), // source: "query_string" or "route"
'total_items' => 3,
'items_per_page' => 1,
'view' => 'pagination/basic',
'auto_hide' => FALSE,
),
);
4. ตัวอย่างการนำไปใช้งานใน Controller
public function action_index() {
// Define our template view and bind to variables
$this->template->content = View::factory('article/index')
->bind('count', $count)
->bind('articles', $articles)
->bind('page_links', $page_links);
// Get the total count of records in the database
$count = DB::select(DB::expr('COUNT(*) AS mycount'))->from('articles')->execute()->get('mycount');
// Create an instance of Pagination class and set values
$pagination = Pagination::factory(array(
'total_items' => $count,
'items_per_page' => 10,
));
$pagination->route_params(array('controller' => $this->request->controller(), 'action' => $this->request->action() ));
// Load specific results for current page
$articles = DB::select()->from('articles')
->order_by('id','ASC')
->limit($pagination->items_per_page)
->offset($pagination->offset)->execute();
// Render the pagination links
$page_links = $pagination->render();
}
5.ตัวอย่างไฟล์ View
<?php defined('SYSPATH') or die('No direct script access.'); ?>
<h1>Kohana Blog Homepage</h1>
<?php echo HTML::anchor("article/new", "New Article"); ?>
<?php foreach ($articles as $article) : ?>
<div class="article">
<div class="title"><?php echo HTML::anchor("article/view/".$article['id'], $article['title']); ?> (<?php echo DB::select()->from('comments')->where('article_id', '=', $article['id'])->execute()->count();?>)</div>
<pre><?php echo $article['content']; ?></pre>
<?php echo HTML::anchor("article/edit/".$article['id'], "Edit"); ?>
<?php echo HTML::anchor("article/delete/".$article['id'], "Delete"); ?>
</div>
<?php endforeach; ?>
<?php echo $count; ?>
<?php echo $page_links; ?>