How to create paging and shorting in cake php

In this post we are going to learn how to create paging and shorting in cakephp list page.

  1. Create controller


class PostsController extends AppController {
var $name = 'Posts';
function inbox() {
$this->paginate = array(
'conditions' => array('Post.status'=>'1'),'limit' => 10,'order' => array('Post.created_date'=>'desc'
$data = $this->paginate('Posts');
$this->set('result', $data);

  1. Create a element file which will show paging in all your cakephp list pages.


echo "<div align='left' class='l1'>".
$this->Paginator->counter(array('format' => 'Showing %page% to %pages% of %count% entries' )).
"</div><div align='right' class='r1'>".
$this->Paginator->first('First', null, null, array('class' => 'disabled'))."&nbsp;&nbsp;&nbsp;".
$this->Paginator->prev('« Prev', null, null, array('class' => 'disabled'))."&nbsp;&nbsp;".
$this->Paginator->next('Next »', null, null, array('class' => 'disabled'))."&nbsp;&nbsp;".
$this->Paginator->last('Last', null, null, array('class' => 'disabled'))."".
  1. Now finaly create cakephp list of posts page


 <table width="100%" style="border: 1px solid #ccc;" border='0'>
if (!empty($result)) {
echo "<tr>";
echo "<td colspan=\"5\" align=\"right\">". $this->element('paging')."</td>";
echo "</tr>";
echo "<tr style=\"background-color: #55699F;\">";
echo "<td><font color=white>S.No</font></td>";
echo "<td><font color=white>".$this->Paginator->sort('Title', 'title')."</font></td>";
echo "<td><font color=white>".$this->Paginator->sort('Description', 'description')."</font></td>";
echo "<td><font color=white>".$this->Paginator->sort('Created Date', 'created_date')."</font>

echo "<td><font color=white>Action</font></td>";
echo "<tr>";
foreach ($result as $resultSet) {?>
<td><?php echo $resultSet['Post']['id'] ?></td>
<td><?php echo $resultSet['Post']['subject']?></td>
<td><?php echo $resultSet['Post']['description']?></td>
<td ><?php echo $message['Post']['created_date']?></td>
<td style="width: 40px"><a onclick="return confirm('Do you really want to delete this message?');" href="delete/<?php echo base64_encode($resultSet['Post']['id']);?>/inbox">Delete</a></td>
echo "<tr>";
echo "<td style=\"height:20px\"></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=\"5\" align=\"right\">". $this->element('paging')."</td>";
echo "</tr>";
} else {?>
<tr >
<td colspan="5">Ther is no posts here.</td>

Hope this post might have cleared your logic about cakephp paging and shorting basics

If you like this post please don’t forget to subscribe My Public Notebook for more useful stuff.