I have a problem in CakePHP 3 to have pagination correctly.
How to get the total number of records retrieved with pagination in CakePHP 3?
I have a problem in CakePHP 3 to have pagination correctly.
How to get the total number of records retrieved with pagination in CakePHP 3?
This might help you. In your controller i.e UsersController.php make pagination like this:
public function index()
{
//set your pagination limit 10
$this->paginate = [
'limit' => 10
];
//query all users
$users = $this->paginate($this->Users->find('all'));
//pass to template
$this->set(compact('users'));
}
In you Template View i.e users/index.ctp put this code to display your pagination
<div class="users index large-9 medium-8 columns content">
<h3><?= __('Users') ?></h3>
<table cellpadding="0" cellspacing="0" border="1">
<thead>
<tr>
<th scope="col"><?= $this->Paginator->sort('id') ?></th>
<th scope="col"><?= $this->Paginator->sort('fullname') ?></th>
<th scope="col"><?= $this->Paginator->sort('email') ?></th>
<th scope="col"><?= $this->Paginator->sort('handphone') ?></th>
<th scope="col" class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?= $this->Number->format($user->uid) ?></td>
<td><?= h($user->fullname) ?></td>
<td><?= h($user->email) ?></td>
<td><?= h($user->handphone) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $user->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $user->id]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->first('<< ' . __('first')) ?>
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('next') . ' >') ?>
<?= $this->Paginator->last(__('last') . ' >>') ?>
</ul>
<p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p>
</div>
Change above variable accordingly to match your table entities (columns' name).
TIPS: If you use CakePHP bake command, you will get all this pagination by default