</> Code The Pixel

CakePHP 4 Print Increment Number With Every Table Row

Asyraf Wahi Anuar - December 22, 2021

Estimated reading time: 42 seconds

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.


Cite this article (APA 6th Edition)

Popular
CakePHP 4 Print PDF Using CakePDF
May, 17 2020
CakePHP 4 Authentication Using Auth...
May, 14 2020
CakePHP 4 Sending Email
February, 01 2022
CakePHP 4 Export To CSV
May, 29 2020
CakePHP 4 jQuery Date Time Picker
October, 01 2018
CakePHP 4 Authentication Using...
May, 11 2020
CakePHP 4 Find, Sort & Count
June, 02 2020
Share
Sharing Link
Click the icon to share