How to display a list in three columns using php?
html, html-table, php
Solution
Try this:
$columns = 3;
$rows = ceil(count($categories) / $columns);
echo '<table>';
for ($row = 0; $row < $rows; $row++) {
echo '<tr>';
foreach ($categories as $k => $category) {
if ($k % $rows == $row) {
echo '<td><a href="category.php?category='.$category["id"].'">'.$category["category"].'</a></td>';
}
}
echo '</tr>';
}
echo '</table>';
It's not very efficient, but right now I can't think on a better way of doing it.
Problem
For the past couple of hours, I've been unsuccessfully trying to figure out the php code to display a list in three columns so that it has this order ``` A D G B E H C F I ``` but I'm really lost. Can anyone help me with this? I currently only have code that lists in this order ``` A B C D E F G H I ``` This is my current code: ``` echo '<table><tr>'; foreach ($categories as $k => $category) { if ($k % 3 == 0 && $k ! = 0) { echo '</tr><tr>'; } echo '<td><a href="category.php?category='.$category["id"].'">'.$category["category"].'</a></td>'; } echo '</table>'; ```