How can the script be modified to display a static numbering on the left side of the table?
To display a static numbering on the left side of the table, you can add a counter variable that increments for each row in the table. This counter can be displayed in a separate column on the left side of the table. By initializing the counter before the loop and incrementing it within the loop, you can ensure that each row is numbered sequentially.
<table>
<tr>
<th>#</th>
<th>Name</th>
<th>Age</th>
</tr>
<?php
$counter = 1;
foreach ($data as $row) {
echo "<tr>";
echo "<td>" . $counter . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "</tr>";
$counter++;
}
?>
</table>
Keywords
Related Questions
- What is the best way to structure a SQL query to retrieve specific data based on user input in PHP?
- How can escaping characters in PHP variables, such as in $_GET['news_id'], help prevent errors and ensure proper functionality?
- In what scenarios would using var_dump() be beneficial for debugging PHP code that involves $_SERVER variables?