cameronmarklewis cameronmarklewis - 1 month ago 14
PHP Question

PHP Foreach Skip First 6

I'm doing a foreach, I would like to skip the first six entries. I have this so far, it's just outputting the same one, for the number of entries left, should I be putting the

if($i2==6) continue;
somewhere else?

<?php
$features = $property->get_features();
if($lastRec2=count($features)){
echo '<div class="row">';
$i2=0;
foreach($features as $feature2) {
$i2++;
if($i2==6) continue;
if( ($i2 % 3== 1) && ($i2<$lastRec2) ) echo '</div><div class="row">';
echo '<div class="col-md-4 p-b-15"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $feature .'</div></div>';
}
echo '</div>';
}
?>

Answer

Just switch to a for loop. Unless you need the iterator.

$features = $property->get_features();
if($lastRec2 = count($features)){
  echo '<div class="row">';
  for ($i2 = 0; $i2 < count($features); ++$i2) {
    if( ($i2 % 3 == 1) && ($i2 < $lastRec2) ) echo '</div><div class="row">';
      echo '<div class="col-md-4 p-b-15"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'.  $features[$i2] .'</div></div>';
    }
    echo '</div>';
  }
Comments