so I have a logic problem here.
what i want:
cicle through an array (actually a foreachable object)
if it's the first element echo a <div>, then every three elements echo the closing
</div>.
If there are fewer than 3, of course end the div too. EDIT: since I was not clear, what I want to achieve is based on n° of elements. Paradoxically I need it to work with 0 elements too.
0 elements:
<div><div>
1 element:
<div>
<a></a>
</div>
3 elements:
<div>
<a></a>
<a></a>
<a></a>
</div>
5 elements:
<div>
<a></a>
<a></a>
<a></a>
</div>
<div>
<a></a>
<a></a>
</div>
etc..... you got the point
my wrong code:
$counter = 0;
foreach ($prods as $prod) {
if($counter == 0 || ($counter % 3) == 0) {
$htm_l .= '<div data-count="' . $counter . '">'; //just keeping track
}
$htm_l .= '<a data-id="' . $prod->id . '"> link </a>';
$counter++;
if($counter == 0 || ($counter % 3) == 0) {
$htm_l .= '</div>';
}
}
$counter = 0;
if($counter == 0 || ($counter % 3) == 0) {
$htm_l .= '</div>';
}
What can I do? (prod is just a collection of objects, could be an array of strings)
$prodsis.