I’m trying to output a 3 column grid of boxes using php by cycling an array. I need to be able to detect the first, 4th, 7th …etc element (i.e each box for every row) and add a class to that box.
for($i = 0; $i < 30; $i++) {
$output .= '<div' . ($i == (1st box in each row) ? ' class="first"' : '') . '>Box ' . $i . '</div>';
}
In my above example I’m looking for the correct code to replace the sudo code in brackets.
You need the modulo operator:
This will be true for 1, 4, 7, 10, etc.