Skip to content
PHP Logo

How can PHP be used to generate a running numerical list for table rows?

To generate a running numerical list for table rows using PHP, you can utilize a loop to iterate through the rows and output the row number for each iteration. This can be achieved by initializing a counter variable outside the loop and incrementing it inside the loop to display the row number.

php
<table>
    <?php
    // Initialize counter variable
    $rowNumber = 1;

    // Loop through table rows
    for ($i = 0; $i < 5; $i++) {
        echo "<tr>";
        echo "<td>" . $rowNumber . "</td>";
        // Add other table data here
        echo "</tr>";
        
        // Increment row number
        $rowNumber++;
    }
    ?>
</table>