How to use pagination in CakePHP Framework

Hi guys! In this tutorial I will explain to you how you can use pagination in CakePHP Framework. I have created also a video where you can see the results!

So, create a table in your database called posts with two columns(id and title) and create a model called Post.php with this content:

<?php

App::uses('Model', 'Model');

class Post extends AppModel {
}

Next, add a new function in your controller:

public function index() {
$this->layout = false;
$this->Post->recursive = 0;
$this->paginate = array(
'limit' => 5
);
$this->set('posts',$this->paginate());
}

This fuunction will display 5 results from your database.

Next, in your view file create a folder Posts and a file index.cpt with this content:

<?php foreach ($posts as $post): ?>
<?php echo $this->Html->link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?><br>
<?php endforeach; ?>
<?php unset($post); ?>
<?php echo $this->Paginator->prev('<< '.__('previous', true), array(), null, array('class'=>'disabled'));?>
<?php echo $this->Paginator->numbers();?>
<?php echo $this->Paginator->next(__('next', true).' >>', array(), null, array('class' => 'disabled'));?>

Video tutorial:









Leave a Comment