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( $userlist as $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>
            <img src="http://bootdey.com/img/Content/user_1.jpg" alt="">
            <a href="<?php echo $i ; // shoud to be 1 ?>" class="user-link">Full name <?php echo $i ; ?></a>
            <span class="user-subhead">Member</span>
        </td>
    <?php 
    if($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:
<?php  for ($i = 5 ; $i<=20; $i+=4){ ?>
                <tr>
                    <td>
                        <img src="http://bootdey.com/img/Content/user_1.jpg" alt="">
                        <a href="<?php echo $i - 4 ; // shoud to be 1 ?>" class="user-link">Full name 1</a>
                        <span class="user-subhead">Member</span>
                    </td>
                    <td>
                        <img src="http://bootdey.com/img/Content/user_1.jpg" alt="">
                        <a href="<?php echo $i - 3; // shoud to be 2 ?>" class="user-link">Full name 1</a>
                        <span class="user-subhead">Member</span>
                    </td>
                    <td>
                        <img src="http://bootdey.com/img/Content/user_1.jpg" alt="">
                        <a href="<?php echo $i - 2; // shoud to be 3 ?>" class="user-link">Full name 1</a>
                        <span class="user-subhead">Member</span>
                    </td>
                    <td>
                        <img src="http://bootdey.com/img/Content/user_1.jpg" alt="">
                        <a href="<?php echo $i - 1; // shoud to be 4 ?>" class="user-link">Full name 1</a>
                        <span class="user-subhead">Member</span>
                    </td>
                </tr>
<?php } ?>
Solution 5:
assume $rows = 20;
<?php  for ($i = 0 ; $i<=$rows; $i++){?>
    <tr>
        <?php if ($i<$rows){ 
        ?>
        <td>
            <img src="http://bootdey.com/img/Content/user_1.jpg" alt="">
            <a href="<?php echo $i ; // shoud to be 1 ?>" class="user-link"><?php echo $rows[$i]['nickname']?></a>
            <span class="user-subhead">Member</span>
        </td>
        <?php } ?>
            <?php   $i = $i+1;
             if ($i<$rows){ ?>
        <td>
            <img src="http://bootdey.com/img/Content/user_1.jpg" alt="">
            <a href="<?php echo $i ; // shoud to be 1 ?>" class="user-link"><?php echo $rows[$i]['nickname']?></a>
            <span class="user-subhead">Member</span>
        </td>
        <?php } ?>
            <?php
            $i = $i+1;
             if ($i<$rows){ ?>
        <td>
            <img src="http://bootdey.com/img/Content/user_1.jpg" alt="">
            <a href="<?php echo $i ; // shoud to be 1 ?>" class="user-link"><?php echo $rows[$i]['nickname']?></a>
            <span class="user-subhead">Member</span>
        </td>
        <?php } ?>
            <?php
            $i = $i+1;
             if ($i<$rows){ ?>
        <td>
            <img src="http://bootdey.com/img/Content/user_1.jpg" alt="">
            <a href="<?php echo $i ; // shoud to be 1 ?>" class="user-link"><?php echo $rows[$i]['nickname']?></a>
            <span class="user-subhead">Member</span>
        </td>
        <?php } ?>
     </tr>
<?php 
if(($i+1)%4==0){ echo "</tr><tr>";}
} ?>     
Post a Comment for "How To Add  After Every Fourth Loop Term Of  "