How can you alternate background colors for every other row in a while loop in PHP?
To alternate background colors for every other row in a while loop in PHP, you can use a counter variable to keep track of the row number. Inside the loop, you can check if the row number is even or odd and apply different background colors accordingly.
<?php
// Assuming $result is the result set from a database query
$row_num = 0;
while ($row = mysqli_fetch_assoc($result)) {
if ($row_num % 2 == 0) {
echo '<div style="background-color: #f0f0f0;">'; // Light background color
} else {
echo '<div style="background-color: #ffffff;">'; // White background color
}
// Output row content here
echo '</div>';
$row_num++;
}
?>
Related Questions
- What are some alternative methods for importing a large database into PHPMyAdmin, especially when traditional methods are slow?
- What are some common pitfalls when comparing encrypted strings in PHP?
- What are the security implications of directly accessing database values in PHP without proper sanitization?