This tutorial will help you to learn how to implement pagination using cakephp framework
Requisite:
- Create a database
- Insert some data in the table
- Controller say UsersController to write the logic.
- Model for the Users table
- View to display the pagination
We will start with creating a UsersController and writing an action in the controller.
path: app/Controller/UsersController.php
<?php
class UsersController extends AppController {
var $uses = array('User');
public $components = array( 'Paginator');
public $helpers = array('Html','Paginator');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('paginateuser');
}
public function paginateuser() {
$paginate = array(
'fields' => array('User.user_name', 'User.email','User.gender','User.state','User.city'),
'order' => array(
'User.user_name' => 'asc')
);
$data = $this->Paginator->paginate();
$this->set('models', $data);
}
}
?>
Create a model for the users table:
path: app/Model/User.php
<?php
class User extends AppModel {
}
?>
Create a view for the action, paginateuser() in the UsersController.php
path: app/View/Users/paginateuser.ctp
<html>
<head><title>simple page</title></head>
<body>
<div>
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Gender</th>
<th>Sate</th>
<th>City</th>
</tr>
</thead>
<tbody>
<?php foreach($models as $val){?>
<tr>
<td><?php echo $val['User']['user_name'];?></td>
<td><?php echo $val['User']['email'];?></td>
<td><?php echo $val['User']['gender'];?></td>
<td><?php echo $val['User']['state'];?></td>
<td><?php echo $val['User']['city'];?></td>
</tr>
<?php }?>
</tbody>
</table>
<tr>
<td>
<?php
echo $this->paginator->prev('<<Previous', null, null, null);
echo " ";
echo $this->paginator->numbers();
echo " ";
echo $this->paginator->next('Next>>', null, null, null);
?>
</td>
</tr>
</div>
</body>
</html>
0 Comment(s)