Skip to content
Advertisement

Doing a line break after X amount of results in a Foreach statement (PHP)

I’ve got a foreach statement that returns say 25 names

foreach($player as $item) {
  echo $player[$count++];
}

How do I make it so after X amount, lets say 10 names, it does a line break? The full code has avatars in i for example, so after the 10th avatar, the 11th one would start after a line break/

foreach($player as $item) {
  echo "<td align='center'><img src='" .  strtr ( $avatar[$count_avatar++], array ("avatar:" => "", ',' => '' )) . "'><br><font>" .  strtr ( $player[$count++], array ("name:" => "", ',' => '' )) . "</font></td><td class='character_spacer'></td>";
}

Advertisement

Answer

<?php
$count = 0;
$row_count = 0;
foreach($player as $item)
{
    $row_count++;
    if ($row_count == 10)
    {
        $row_count = 0;
        echo '<br />'; // It will not work within tr or td you have to add margins here according to your need.
    }
    $count++;
}
?>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement