简体   繁体   中英

How to add <tr></tr> after every fourth loop term of <td></td>

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 the loop after every 4 th number of loop. My one tr will contain 4 td and every td value is incremented with respect to the loop like the first td will 1 second will 2 and last will 4, then it should break with a new tr and the value is in td like I said in increment order.

My code is below:

 <?php  for ($i = 0 ; $i<=20; $i++){?>
     <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 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 ; // 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 ; // 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 ; // shoud to be 4 ?>" class="user-link">Full name 1</a>
             <span class="user-subhead">Member</span>
         </td>

      </tr>

 <?php 
if($i%4==0){    echo "<tr></tr>";}


} ?>

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>'; }
    ?>

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>';
<?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 } ?>

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

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>";}

} ?>     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM