How can PHP be used to display results in rows of 3 in a table format?
To display results in rows of 3 in a table format using PHP, you can use a loop to iterate through the results and create a new row every 3 items. You can achieve this by keeping track of the count of items displayed and inserting a new row when the count reaches 3.
<?php
$results = ["Result 1", "Result 2", "Result 3", "Result 4", "Result 5", "Result 6"];
echo "<table>";
$count = 0;
foreach ($results as $result) {
if ($count % 3 == 0) {
echo "<tr>";
}
echo "<td>$result</td>";
$count++;
if ($count % 3 == 0) {
echo "</tr>";
}
}
echo "</table>";
?>
Keywords
Related Questions
- Are there any best practices for ensuring that table backgrounds are printed correctly when using PHP?
- What potential pitfalls should be considered when using PHP to automatically select values in dropdown menus based on user data?
- What is the importance of using htmlspecialchars() for variables values when outputting them in HTML in PHP?