2

I have this loop in my code:

$results = modleftHelper::getNear();
    $i = 0;
    foreach($results as $result){
        echo '<div class="filter-div" data-filter="'.$result->near.'">';
        echo '<label id="lbl_type'.$i.'" class="label_check" name="near" style="width:230px !important;">';
        echo '<input name="school[]" class="chkBX" value="'.$result->latitude.','.$result->longitude.'" type="checkbox" />'.$result->school.'</label>';
        echo '</div>';
    $i++;
}

I want to add clear class every 3, 6, 9 etc in this div:

<div class="filter-div" data-filter="'.$result->near.'">';

Any ideas or suggestions? Thanks.

2
  • if($i%3==0) will do the trick Commented Sep 13, 2013 at 5:37
  • Seems like intervals of three. Why not mod 3? $i%3 Commented Sep 13, 2013 at 5:37

5 Answers 5

2

Try this:

$results = modleftHelper::getNear();
    $i = 0;
    foreach( $results as $result ) {
        if ( $i % 3 == 0 ) {
           echo '<div class="filter-div clear" data-filter="'.$result->near.'">';
        } else {
           echo '<div class="filter-div" data-filter="'.$result->near.'">';
        }
        echo '<label id="lbl_type'.$i.'" class="label_check" name="near" style="width:230px !important;">';
        echo '<input name="school[]" class="chkBX" value="'.$result->latitude.','.$result->longitude.'" type="checkbox" />'.$result->school.'</label>';
        echo '</div>';
    $i++;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try like

$i = $j = 0;
foreach($results as $result){

    if($i++%3==0)
    {
      echo '<div class="filter-div clear" data-filter="'.$result->near.'">';          
    } else {
        echo '<div class="filter-div" data-filter="'.$result->near.'">';
    }

    echo '<label id="lbl_type'.$j++.'" class="label_check" name="near" style="width:230px !important;">';
    echo '<input name="school[]" class="chkBX" value="'.$result->latitude.','.$result->longitude.'" type="checkbox" />'.$result->school.'</label>';
    echo '</div>';
}

Consider that Iam using $i for checking the condition and $j for the loop variable

Comments

0

This will be helpful

$results = modleftHelper::getNear();
    $i = 0;
    foreach($results as $result){
        $i++;

        //check the incremental value
        if($i%3==0)
        {
          echo '<div class="filter-div" data-filter="'.$result->near.'">';
      $i=0; 
        }else{
        //add youe false condition ghere
            echo '<div class="filter-div" data-filter="">';
    }

        echo '<label id="lbl_type'.$i.'" class="label_check" name="near" style="width:230px !important;">';
        echo '<input name="school[]" class="chkBX" value="'.$result->latitude.','.$result->longitude.'" type="checkbox" />'.$result->school.'</label>';
        echo '</div>';

}

Comments

0

Try this:

$results = modleftHelper::getNear();
$i = 0;
foreach($results as $result){
    $clear = ($i+1 % 3) == 0 ? "clear" : "";
    echo '<div class="filter-div' . ' ' . $clear .'" data-filter="'.$result->near . '">';
    echo '<label id="lbl_type'.$i.'" class="label_check" name="near" style="width:230px !important;">';
    echo '<input name="school[]" class="chkBX" value="'.$result->latitude.','.$result->longitude.'" type="checkbox" />'.$result->school.'</label>';
    echo '</div>';
$i++;

}

Comments

0
$results = modleftHelper::getNear();
    $i = 0; 
    foreach($results as $result){
        if($i%3==0){$last_class="clear";}else{$last_class="";}
        echo '<div class="filter-div '.$last_class.'" data-filter="'.$result->near.'">';
        echo '<label id="lbl_type'.$i.'" class="label_check" name="near" style="width:230px !important;">';
        echo '<input name="school[]" class="chkBX" value="'.$result->latitude.','.$result->longitude.'" type="checkbox" />'.$result->school.'</label>';
        echo '</div>';
   $i++;
}

Try this code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.