Skip to content

Instantly share code, notes, and snippets.

@FrankM1
Created August 27, 2013 06:23
Show Gist options
  • Save FrankM1/6350229 to your computer and use it in GitHub Desktop.
Save FrankM1/6350229 to your computer and use it in GitHub Desktop.
Grouping items in a foreach loop
<?php
$i = 0;
$group = 0;
$group_by = 5;
while ($i < 19){
$i++;
if ($group == 0) {//new group start
echo "<li>";
}
echo "<div>item " . $i . "</div>";
$group++;
if ($group == $group_by) {//end of group
echo "</li>\n";
$group = 0;
$group_by = 6;
}
}
if ($group != 0) { echo "</li>"; }
@FrankM1
Copy link
Author

FrankM1 commented Aug 27, 2013

Output:

  • item 1
    item 2
    item 3
    item 4
    item 5
  • item 6
    item 7
    item 8
    item 9
    item 10
    item 11
  • item 12
    item 13
    item 14
    item 15
    item 16
    item 17
  • item 18
    item 19
  • Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment