Achieving Pagination in Cakephp is Slight simple. We use the initial method for applying the pagination.Here i use the Helper to load a paginate class and its instantiate part by using $paginate variable in Controller.
For this first up all, we define the Helper variable like,
In View Page (views/news/index.ctp page), we add the following code for display the data with Pagination.
For this first up all, we define the Helper variable like,
var $name='news';//init for news.
var $helpers=array('Html','Session'); // Load the Helpers to an array.
var $paginate=array('limit' =>5, 'order'=> array('News.id'=>'asc')); //init for Paginate and its options.
// Then we make the Controller - newsController - Code
function index(){
$data=$this->$paginate('News');
//Apart from find(); function we use the paginate();
$this->set('news',$data);
}
In View Page (views/news/index.ctp page), we add the following code for display the data with Pagination.
echo $html->css('default'); // if need to load Style sheet.
echo $html->link(
'Add New News',
array(
'controller' => 'News',
'action' => 'add',
),
array(
),
null
);
echo $html->tableHeaders(
array(
'ID',
'Title',
'News',
'Comments'
)
);
foreach($news as $news)
{
echo $html->tableCells(
array(
array(
$news['News']['id'],
$news['News']['n_title'],
$news['News']['n_news'],
$news['News']['n_comments']
)
)
);
}
echo $html->div(
null,
$paginator->prev(
'<< Previous',
array(
'class' => 'PrevPg'
),
null,
array(
'class' => 'PrevPg DisabledPgLk'
)
).
$paginator->numbers().
$paginator->next(
'Next >>',
array(
'class' => 'NextPg'
),
null,
array(
'class' => 'NextPg DisabledPgLk'
)
),
array(
'style' => 'width: 100%;'
)
);
It displays the data with Pagination. Displays first 5 rows as per set the limit by 5.