doubu1950 2015-09-26 08:04
浏览 489
已采纳

如何在<td> </ td>的每第四个循环项后添加<tr> </ tr>

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


} ?>
  • 写回答

5条回答 默认 最新

  • dongliu8542 2015-09-26 08:53
    关注

    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>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)