CakePHP Pagination Helper

Pagination is one of the vital components to manage limit of record to be view and navigation to the next or previous page. Technically, the pagination in CakePHP is based on the page number with next and previous navigation. However, in some cases, we need to have a go to first page or go to last page button for faster navigation. This tutorial contains few sections.

Figure 1: Pagination at the end of list


Section I: Limit Per Page
To limit the number of rows per page, a simple modification to respective controller is necessary. The pagination needs to be identified for example, 10 rows of record per page as shown below. Once the record reach to 10, the next record (11) will be display in second page. A page navigation will be generated at the end of the rows.

public function index() {
	$this->paginate = array(
		'maxLimit' => 10);
	$this->User->recursive = 0;
	$this->set('users', $this->paginate());
}


Section II: Limit Per Page with Single Condition
With the maximum limit set to 10 row of records per page, developer can filter the list to show based on condition for example only record with status == Active only will appear in the list. The code shows that in the array, there is 'condition' to capture the filter.

public function index() {
	$this->paginate = array(
		'maxLimit' => 10,
		'conditions' => array('User.status' => 'Active'));
	$this->User->recursive = 0;
	$this->set('users', $this->paginate());
}


Section III: Limit Per Page with Multiple Condition
If you have more than one condition simply set as shown below:

public function index() {
	$this->paginate = array(
		'maxLimit' => 10,
		'conditions'=>array(
			'User.status' => 'Active',
			'User.role' => 'Administrator','Moderator'));
	$this->User->recursive = 0;
	$this->set('users', $this->paginate());
}


Section IV: Limit Per Page with Single Condition and Order by
To sort the records, use 'order' method as shown below to sort the record ascendingly (asc) or descending (desc).

public function index() {
	$this->paginate = array(
		'maxLimit' => 10,
		'conditions'=>array('User.status' => 'Active',
		'order' => array('User.created' => 'asc'))); //asc || desc
	$this->User->recursive = 0;
	$this->set('users', $this->paginate());
}


Section V: Limit Per Page and Only Owner Record Listed
To filter record that only belongs to the owner, 1) Authentication is required; 2) The ID of the owner need to be save in respective field for example in documents table, it should contain user_id; 3) Current user auth session. The condition should be set as shown below to filter record that only have equal value with current auth session ID.

public function index() {
	$this->paginate = array(
		'maxLimit' => 10,
		'conditions'=>array('Document.user_id' => $this->Auth->user('id')));
	$this->Document->recursive = 0;
	$this->set('documents', $this->paginate());
}


Section VI: Priority Sort
To sort based on priority for example you have three (3) types of users such as 1) administrator; 2) moderator; 3) staff and the list should prioritize administrator first and follow by moderator and staff. The coding should be as follows:

public function index() {
	$this->paginate = array(
		'maxLimit' => 10,
		'conditions'=>array('User.status' => 'Active',
		'order' => "FIELD"(User.role, 'Administrator', 'Moderator', 'Staff'));
	$this->User->recursive = 0;
	$this->set('users', $this->paginate());
}


Section VII: First and Last Record Navigation
The pagination function will show a limited number of pages at once for example page 1 to 9 and if you have 50 pages, it required you to click few times to page 9, 18, 27, 36, 45 and you'll get the page 50. To skip this step, developer can create a Start and End button at the pagination by using the $paginator function. In your controller, set the paginate parameter as shown below:

public function index() {
	$this->paginate = array(
		'maxLimit' => 10);
	$this->User->recursive = 0;
	$this->set('users', $this->paginate());
}


Navigate to view.ctp for respective model, and add the $paginator before the foreach as show below:

<?php $paginator = $this->Paginator; ?>
	<?php foreach ($users as $user): { ?>	


To create a navigation button for First and End of record, add the following code in pagination element:

<?php 
	echo $paginator->first('First',array('class' => 'next','tag' => 'li','escape' => false));
	echo $paginator->last('Last',array('class' => 'next','tag' => 'li','escape' => false));
?>


or else you can combine them as shown below:

<ul class="pagination pagination-sm">
	<?php
		echo $paginator->first('First',array('class' => 'next','tag' => 'li','escape' => false));
		echo $this->Paginator->prev('&larr; Previous', array('class' => 'prev','tag' => 'li','escape' => false), '<a onclick="return false;">&larr; Previous</a>', array('class' => 'prev disabled','tag' => 'li','escape' => false));
		echo $this->Paginator->numbers(array('separator' => '','tag' => 'li','currentClass' => 'active','currentTag' => 'a'));
		echo $this->Paginator->next('Next &rarr;', array('class' => 'next','tag' => 'li','escape' => false), '<a onclick="return false;">Next &rarr;</a>', array('class' => 'next disabled','tag' => 'li','escape' => false));
		echo $paginator->last('Last',array('class' => 'next','tag' => 'li','escape' => false));
	?>			
</ul>


The output should display as follows:

Figure 2: Pagination with First and End navigation


That all guys. Happy coding :)