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

PostsController.php

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.

Views/Elements/paging.ctp

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->numbers()."&nbsp;&nbsp;".
$this->Paginator->next('Next »', null, null, array('class' => 'disabled'))."&nbsp;&nbsp;".
$this->Paginator->last('Last', null, null, array('class' => 'disabled'))."".
"</div>";
  1. Now finaly create cakephp list of posts page

Views/Posts/summary.ctp

 <table width="100%" style="border: 1px solid #ccc;" border='0'>
<?php
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>
/*
Where Created Date will display on summary page and created_date is posts database column. 
*/
</td>";
echo "<td><font color=white>Action</font></td>";
echo "<tr>";
foreach ($result as $resultSet) {?>
<tr>
<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>
</tr>
<?php
}
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>
</tr>
<?php
}
?>
</table>

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.

One Reply to “How to create paging and shorting in cake php”

  1. Please I requested to you provide a date paginator.
    I am not able to make or find from internet.I hope you will care about it.
    Thanku

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.