How can you alternate the styling of every second row in a table generated by PHP?
To alternate the styling of every second row in a table generated by PHP, you can use a simple conditional statement within a loop that iterates over the rows of the table. By checking if the row index is even or odd, you can apply different CSS classes to style the rows accordingly.
<table>
<?php
$rows = 10; // Number of rows in the table
for ($i = 1; $i <= $rows; $i++) {
if ($i % 2 == 0) {
echo '<tr class="even">';
} else {
echo '<tr class="odd">';
}
echo '<td>Row ' . $i . '</td>';
echo '</tr>';
}
?>
</table>
<style>
.even {
background-color: lightgray;
}
.odd {
background-color: white;
}
</style>
Related Questions
- How can one effectively utilize subqueries in PHP when working with virtual tables?
- What are the advantages and disadvantages of using TinyMCE versus CKEditor for PHP projects?
- What considerations should be taken into account when integrating JavaScript functionalities, such as dynamic key generation, into PHP scripts for web applications like key distribution systems?