Skip to content Skip to sidebar Skip to footer

How To Add After Every Fourth Loop Term Of

I want to break from the loop after every fourth term of number in the loop. I want to create a list of twenty people; in every tr should to be 4 people. So, I want to break from t

Solution 1:

Just wanted to stick my neck out and show an alternative aproach. Normally you have an array of users. This one will create a table with 4 cols, if you have 22 users only 20 will show in this example as it limits output to fill up each row.

$td = array();
$userlist = array( 'Bob', 'John', 'Robert', 'Eric', 'Lydia', 'Fanny', 'Alex', 'Leopold', 'Tom', 'Mark', 'Bob2', 'John2', 'Robert2', 'Eric2', 'Lydia2', 'Fanny2', 'Alex2', 'Leopold2', 'Tom2', 'Mark2' );

foreach( $userlistas$i => $user ) {

    if ( $i != 0 && $i%4 == 0 ) {
      $td[] = '<td> ' . implode( '</td><td>', $tdata ) . '</td>';
      $tdata = array();
    }

    $tdata[] = '<img src="http://bootdey.com/img/Content/user_1.jpg" alt="">
                <a href="' . $i . '" class="user-link">Full name ' . $user . '</a>
                <span class="user-subhead">Member</span>'; 

}

echo'<table><tr>' . implode( '</tr><tr>', $td ) . '</tr></table>';

Solution 2:

You can add dummy variable $temp, then increment the value at each time if you get forth it will again starts with 1.

this is easiest way to do!!

<?php$temp =1;
    for ($i = 1 ; $i<=23; $i++){ if($temp == 1){   echo"<tr>"; } ?><td><imgsrc="http://bootdey.com/img/Content/user_1.jpg"alt=""><ahref="<?phpecho$i ; // shoud to be 1 ?>"class="user-link">Full name <?phpecho$i ; ?></a><spanclass="user-subhead">Member</span></td><?phpif($temp == 4){ echo"</tr>"; $temp = 0; }
        $temp++;
    }
  if($temp-1 != 0 ){ echo'</tr>'; }
    ?>

Solution 3:

change if($i%4==0){ echo "<tr></tr>";} to if(($i+1)%4==0){ echo "</tr><tr>";}

Solution 4:

<?phpfor ($i = 5 ; $i<=20; $i+=4){ ?><tr><td><imgsrc="http://bootdey.com/img/Content/user_1.jpg"alt=""><ahref="<?phpecho$i - 4 ; // shoud to be 1 ?>"class="user-link">Full name 1</a><spanclass="user-subhead">Member</span></td><td><imgsrc="http://bootdey.com/img/Content/user_1.jpg"alt=""><ahref="<?phpecho$i - 3; // shoud to be 2 ?>"class="user-link">Full name 1</a><spanclass="user-subhead">Member</span></td><td><imgsrc="http://bootdey.com/img/Content/user_1.jpg"alt=""><ahref="<?phpecho$i - 2; // shoud to be 3 ?>"class="user-link">Full name 1</a><spanclass="user-subhead">Member</span></td><td><imgsrc="http://bootdey.com/img/Content/user_1.jpg"alt=""><ahref="<?phpecho$i - 1; // shoud to be 4 ?>"class="user-link">Full name 1</a><spanclass="user-subhead">Member</span></td></tr><?php } ?>

Solution 5:

assume $rows = 20;

<?phpfor ($i = 0 ; $i<=$rows; $i++){?><tr><?phpif ($i<$rows){ 

        ?><td><imgsrc="http://bootdey.com/img/Content/user_1.jpg"alt=""><ahref="<?phpecho$i ; // shoud to be 1 ?>"class="user-link"><?phpecho$rows[$i]['nickname']?></a><spanclass="user-subhead">Member</span></td><?php } ?><?php$i = $i+1;

             if ($i<$rows){ ?><td><imgsrc="http://bootdey.com/img/Content/user_1.jpg"alt=""><ahref="<?phpecho$i ; // shoud to be 1 ?>"class="user-link"><?phpecho$rows[$i]['nickname']?></a><spanclass="user-subhead">Member</span></td><?php } ?><?php$i = $i+1;

             if ($i<$rows){ ?><td><imgsrc="http://bootdey.com/img/Content/user_1.jpg"alt=""><ahref="<?phpecho$i ; // shoud to be 1 ?>"class="user-link"><?phpecho$rows[$i]['nickname']?></a><spanclass="user-subhead">Member</span></td><?php } ?><?php$i = $i+1;
             if ($i<$rows){ ?><td><imgsrc="http://bootdey.com/img/Content/user_1.jpg"alt=""><ahref="<?phpecho$i ; // shoud to be 1 ?>"class="user-link"><?phpecho$rows[$i]['nickname']?></a><spanclass="user-subhead">Member</span></td><?php } ?></tr><?phpif(($i+1)%4==0){ echo"</tr><tr>";}

} ?>

Post a Comment for "How To Add After Every Fourth Loop Term Of "