CakePHP 4 Print Increment Number With Every Table Row
Increment number in every table row is very useful in some web application projects. This tutorial will show how to set a maximum limit of rows per page with an incrementing number in every row.
This tutorial may be outdated
It was last reviewed 4 years ago. Some steps may differ in newer versions.
Tech stack
- CakePHP 4.x
Increment number in every table row is very useful in some web application projects. This tutorial will show how to set a maximum limit of rows per page with an incrementing number in every row. If we set the maximum number of rows per page is 10, the first page should print the row number from 1 to 10 and on the second page, it should print 11 to 20.
Defined the maxLimit
In your controller, define the maxLimit as shown below:
$this->paginate = [
'maxLimit' => 10
];Print rows number
In your view (eg: index), include the following code:
<?php
$page = $this->Paginator->counter('{{page}}');
$limit = 10;
$counter = ($page * $limit) - $limit + 1;
?>
<?php foreach ($articles as $article) : ?>
<tr>
<td><?= $counter++ ?></td>This will print the row number based on the current page.
Did this tutorial help?
220 readers found this helpful
Comments
No comments yet. Questions about this tutorial are welcome here: the answer helps the next reader too.