PHP - Adding divs to a foreach loop every 4 times
$count = 1;
foreach( $users_kicks as $kicks )
{
if ($count%4 == 1)
{
echo "<div>";
}
echo $kicks->brand;
if ($count%4 == 0)
{
echo "</div>";
}
$count++;
}
if ($count%4 != 1) echo "</div>"; //This is to ensure there is no open div if the number of elements in user_kicks is not a multiple of 4
This answer is very late - but in case people see it - this is a cleaner solution, no messy counters and if
statements:
foreach (array_chunk($users_kicks, 4, true) as $array) {
echo '<div>';
foreach($array as $kicks) {
echo $kicks->brand;
}
echo '</div>';
}
You can read about array_chunk on php.net