How can the issue of unnecessary new table rows after every specified number of columns be resolved in PHP?

Issue: The problem of unnecessary new table rows being created after every specified number of columns can be resolved by using a counter variable to keep track of the number of columns added. When the counter reaches the specified number of columns, a new table row should be started to prevent additional columns from being added to the current row. PHP Code Snippet:

<?php
$columns = 3; // Specify the number of columns before starting a new row
$counter = 0; // Initialize counter variable

echo '<table>';
for ($i = 1; $i <= 10; $i++) { // Loop through 10 items as an example
    if ($counter % $columns == 0) {
        echo '<tr>'; // Start a new row
    }
    
    echo '<td>Column ' . $i . '</td>'; // Add column data
    
    $counter++; // Increment counter
    
    if ($counter % $columns == 0) {
        echo '</tr>'; // End the row if the specified number of columns is reached
    }
}
echo '</table>';
?>