The following loop groups the items into two groups: First group of 5 items in a li and then each group of 6 items. I’m trying to modify this to group first 3 items in a li and then next each 5 items in the li.
for ($i = 1; $i < 20; $i++) {
echo ($i === 1 || $i % 6 === 0) ? "<li>" : null,
"<div>item {$i}</div>",
($i % 6 === 5) ? "</li>" : null;
}
if ($i % 6 !== 0) echo "</li>";
I’m trying to modify to following, but the grouping does not work fine (eg. wrong number of items goes in a li, some items do not get in li etc.)
for ($i = 1; $i < 20; $i++) {
echo ($i === 1 || $i % 5 === 0) ? "<li>" : null,
"<div>item {$i}</div>",
($i % 5 === 3) ? "</li>" : null;
}
if ($i % 5 !== 0) echo "</li>";
Try this (Edited):
Code example with slight changes
Explanation:
If you want to group items in groups of 5, then mod by 5. The first three items are a special case, but since 3 is less than 5, we can incorporate the first three numbers into our main loop.
The problem with your code is in adding the
<li>s. Since we start$iat 1 instead of 0, we should use$i % 5 == 4instead of$i % 5 == 0.