A loop (while/foreach) with "offset" wrapping and

Posted by DarkGhostHunter on Stack Overflow See other posts from Stack Overflow or by DarkGhostHunter
Published on 2012-06-30T02:33:02Z Indexed on 2012/06/30 3:16 UTC
Read the original article Hit count: 136

Filed under:
|
|
|
|

After applying what wrapping objects using math operator, I just tought it will be over. But no. By far.

<?php
$faces= array(
  1 => '<div class="block">happy</div>',
  2 => '<div class="block">sad</div>',
  (sic)
  21 => '<div class="block">angry</div>'
);

$i = 1;
foreach ($faces as $face) {
  echo $face;
  if ($i == 3) echo '<div class="block">This is and ad</div>';
  if ($i % 3 == 0)  {
    echo "<br />"; // or some other wrapping thing
  }
  $i++;
}

?>

In the code I have to put and ad after the second one, becoming by that the third object. And then wrap the three all in a <div class="row"> (a br after won't work out by design reasons). I thought I will going back to applying a switch, but if somebody put more elements in the array that the switch can properly wrap, the last two remaining elements are wrapped openly.

Can i add the "ad" to the array in the third position? That would make things simplier, only leaving me with guessing how to wrap the first and the third, the fourth and the sixth, an so on.

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays