CakePHP Pagination

The development of a user-friendly interface is one of the main issues for the web application developers because when the user writes their source code, then the length of code and complexity increased. Also, the managing of hundreds or thousands records on a single page was not easy to handle. Furthermore, it takes a lot of time to fetch the details on a single page due to which the web application suffers their reliability and user satisfaction. So, cakephp developers provide a pagination function to overcome from the hundreds of records in a single page.

We have listed a few issues which as follows

If we want to show user's activity record on a single page, then the length of the records is too lengthy due to which we have faced these issue -

  • The user needs to scroll multiple times to view the information. 
  • It will not be easy to search the website on portable devices such as mobile devices, tablets, etc.
  • It will decrease the web application performance.

From these situations, CakePHP introduces a concept pagination method.

Pagination method of CakePHP: It divides all records into some equal parts and shows a single record to the user according to the need of the application.

 Example: suppose we have 100 records on each page and we want to show only 10 records. Like a simple maths to get the full sheets, it's 100/10 = 10. Due to this, we need to provide a way to the user to view the details on each page either in small screen like as mobile or desktop device.

Example:

Here we have created a file of controller TutorialsController.php file 

 <?php
 use namespace App\Controller;
 class TutorialsController extends AppController
 { 
  public function initialize()
  {
  parent::initialize();
  $this->loadComponent('Flash'); // Include the FlashComponent
  $this->loadComponent('Paginator'); // it will load Paginator
  }
  public function index()
  { 
   $this->paginate = [ // here we have define limit of the record on the page
  'limit' => '3'
  ];
  $this->set('tutorials', $this->paginate($this->Tutorials->find('all')));
 }
 }
 ?> 

Here, we have created an index.ctp file in which we have defined the pagination links to move either the first page or the last page.

 <h2>hello</h2>
 <table>
  <tr>
  <th>Id</th>
  <th>Name</th>
  <th>Company</th>
  <th>Email</th>
  <th>City</th>
  <th>Action</th>
  </tr>
  <?php foreach ($tutorials as $row): ?>
  <tr>
  <td><?= $row->id ?></td
  <td> <?= $row->name ?></td>
  <td> <?= $row->company ?></td>
  <td> <?= $row->email ?></td>
  <td><?= $row->city ?></td>
  <?php endforeach; ?>
 <?= $this->Html->link('Add Employee', ['action' => 'add']) ?>
 </table>
 <?php
 $paginator = $this->Paginator;
 echo "<div class = 'paging'>";
 //for the first page link // the parameter’First’ is the labeled, same with other pagination link
 echo $paginator->first('First');
 echo " ";
 //if there was previous records, prev link will be displayed
 if($paginator->hasPrev()){
 echo $paginator->prev('<<');
 }
 //modulus => 3 specifies how many page number will be displayed
 echo $paginator->numbers(array('modulus' =>3)); 
 if($paginator->hasNext()){ //there are records, next link will be displayed
 echo $paginator->next('>>');
 }
 echo $paginator->last('Last'); //for the last page link
 echo "</div)";
 ?> 

Now run your program in localhost like: localhost/my_app_name/tutorials. Then it will show the given Output.

CakePHP Pagination

In the above output, we have created a pagination link in which we set a condition for showing 3 records in a table. However, there is a link that moves your page either on the first page or last page.

Field parameter: In this case, we can set the attributes name with the Controller through which we can show particular items on every web page.

Limit: we can set a maximum10 link on any page for showing the details.

The MaxLimit states that its value will never go out from this range. By default, CakePHP sets the maximum number of rows that can be retrieved up to 100. If it doesn’t choose default for your application, you can adjust it as part of the pagination options, e.g., by reducing it to 10: 

 public $paginate = [
  // Other keys here.
  'maxLimit' => 10
 ]; 

If the request’s limit parameter is greater than 10, it can automatically set the maxLimit value.

We can arrange the list of items, either the ascending or descending order in the web page. Also, we can show the particular fields in the web page. Like, in given below function of pagination.

  $this->paginate = [
  'fields' => ['Tutorials.id', 'Tutorials.company'], // here we have define which field //we want to show
  'limit' => '3',  
  'order' => [ // we arrange the record either we ‘asc’ or ‘desc’
  'Tutorials.id' => 'asc' // we can arrange the column record by ‘asc’ or ‘desc’
 ] ]; 
  $this-> set (‘tutorials’, $this -> paginate( $this ->Tutorials ->find( ‘all’))); 

Paginating in different model

We can set the same pagination method in different model by use of given below query in our controller, that call the model’s table object, or simply its name:

 // Using a query
 $posts = $this->paginate($commentsTable->find());
 // Using the model name.
 $posts = $this->paginate('Posts');
 // Using a table object.
 $posts = $this->paginate($Table); 

You can use the PaginatorComponent directly if you need to paginate information from another component. It has a similar API to the controller process:

 $tutorials = $this->Paginator->paginate($TutorialTable->find(), $config);
 // Or
 $tutorials = $this->Paginator->paginate($TutorialTable, $config); 

A first parameter is a query object which finds the details of the records from the table object, and the second parameter is used for passing the key-value pair of data for showing the pagination in an array format. The array should define the same properties as we declared as the $paginate property on a controller.

For showing the page out of range

The PaginatorComponent will through an error when the user tried to access the page that is not available on the web, or it will through an error when the requested page is higher than the defined page number in your paginator. Furthermore, if you want to show a standard message on your web browser when the page is not available, then you have to use a try-catch block and take an appropriate action when NotFoundException is found. You have to use the given below syntax in your Controller.

 use Cake\Http\Exception\NotFoundException;
 public function index()
 {
  try {
  $this->paginate( $this ->Tutorials ->find( ‘all’)));
  } catch (NotFoundException $e) {
  // create a link to move the first and last page.
  // $this->request->getParam('paging') will give you required info.
  }
 }  

When you will use this code in your Controller then it will throw an error because you are going out of the page range in the application.