If you have some counter in your loop you can use Modulus for this.
It's basically what's left of a number if you divide it.
Example:
for($i = 1; $i < 11; $i++) {
if ($i % 2 === 0) {
print('this is printed every two times');
}
if ($i % 3 === 0) {
print('this is printed every three times');
}
}
If you use a foreach() in stead you should just make a counter yourself (as Link stated you could also use the key of an array if it contains nice incremental keys):
$i = 1;
foreach($array as $item) {
if ($i % 2 === 0) {
print('this is printed every two times');
}
if ($i % 3 === 0) {
print('this is printed every three times');
}
$i++;
}
Or in your specific case it would look something like:
print('
');$i = 1;
foreach($array as $item) {
if ($i % 3 === 0) {
print("
\n");}
print("
$item\n");$i++;
}
print('
');The above is just a basic example.
You should also check whether the number of the columns is balanced and if not either add a colspan or an empty columns to balance it.