Category Archives: Cake PHP

Ajax pagination with jQuery in cakephp 2.x.x

For apply ajax pagination in cakephp 2.x.x follow these below steps:


1). Copy File JqueryEngineHelper.php From lib/Cake/View/Helper and Paste to  app/View/Helper.

2). Include Latest jQuery File to your layout file for ajax paging.

ex- in app/View/Layouts/ajaxpagination.ctp

<!DOCTYPE html>

<!– This is mandotary –>
<?php echo $this->Html->script(‘;); ?>
<div id=”spinner” style=”display: none; float: right;”>
<?php echo $this->Html->image(‘indicator.gif’, array(‘id’ => ‘busy-indicator’)); ?>
<div id=”content”>
<?php echo $content_for_layout; ?>


NOTE: Here ‘indicator.gif’ image is a loader can place this image in app/webroot/img directory.

3).  To make Javascript enhanced links instead of plain HTML links, you have to use options() parameter to paginator helper .

ex- In your view ctp file add the following:

<!– This is mandotary –>
<?php $this->paginator->options(array(‘update’ => ‘#content’,’before’ => $this->Js->get(‘#spinner’)->effect(‘fadeIn’, array(‘buffer’ => false)),’complete’ => $this->Js->get(‘#spinner’)->effect(‘fadeOut’, array(‘buffer’ => false))));?>

Showing Page <?php echo $this->paginator->counter(); ?>
<th><?php echo $this->paginator->sort(‘Post.title’, ‘Title’);?></th>
<th><?php echo $this->paginator->sort(‘Post.created’, ‘Created Date’);?></th>
<?php foreach($customers as $customer): ?>
<td style=”padding-right: 30px;”><?php echo $customer[‘Post’][‘title’]; ?></td>
<td style=”padding-left: 30px;”><?php echo $customer[‘Post’][‘created’]; ?></td>
<?php endforeach; ?>

<?php echo $this->paginator->prev(); ?> – &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<?php echo $this->paginator->numbers(array(‘separator’=>’ – ‘)); ?> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<?php echo $this->paginator->next(‘Next Page’); ?>
<?php echo $this->Js->writeBuffer();?> <!– This is mandotary –>

At the bottom of your view file. Be sure to include:

<?php echo $this->Js->writeBuffer();?>

If you omit this you will not be able to chain ajax pagination links. When you write the buffer, it is also cleared, so you don’t have worry about the same Javascript being output twice.

4). Controller File. add the following lines to your controller action:

$this->layout = ‘ajaxpagination’;
$this->set(‘customers’, $this->paginate(‘Post’)); <!– Record from the model which you want to show in view file with pagination. –>


Posted by on Feb 7, 2013 in Cake PHP, PHP


Tags: , , , , , , , ,

CONCAT in query , Virtual Fields and make a drop down

I need to make a dropdown list of Locations. In dorpdown i need to put location name and location state name. I already have a column in database name which contain location name and location state name. Here is the steps to do so:

In Location Model(in which database table locations are saved) write the following code

1 var $virtualFields = array('dropdown_name' => 'CONCAT(Location.location_name, " ", Location.location_state)');
In Controller in which you want to show dropdown write this(eg: ShowsController):
2 $this->loadModel('Location');
$loc_list = $this->Location->find("list", array("fields" => array("location_id", 'location_name')));
In view template(eg. View/Shows/add.php) add the following
2 $this->loadModel('Location');
$loc_list = $this->Location->find("list", array("fields" => array("location_id", 'location_name')));
Leave a comment

Posted by on May 14, 2012 in Cake PHP, PHP


Tags: , , , ,

%d bloggers like this: