如何添加< tr>< / tr>在< td>< / td>的每第四个循环项之后, [英] How to add <tr></tr> after every fourth loop term of <td></td>

查看:146
本文介绍了如何添加< tr>< / tr>在< td>< / td>的每第四个循环项之后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想创建一个二十个人的列表,在每一个 tr 应该是4个人。



所以,我想每隔4 <循环次数。我的 tr 将包含4 td 和每个 td 值是相对于第一个 td 的循环递增,1秒将是2并且最后是4,那么它应该与新的 tr ,这个值在 td 中,就像我在递增顺序中所说的那样。

我的代码如下:

 <?php for($ i = 0; $ i <= 20; $ i ++){?> 
< tr>

< td>
< img src =http://bootdey.com/img/Content/user_1.jpgalt =>
< a href =<?php echo $ i; // shoud to 1?> class =user-link>全名1< / a>
< span class =user-subhead>成员< / span>
< / td>
< td>
< img src =http://bootdey.com/img/Content/user_1.jpgalt =>
< a href =<?php echo $ i; // shoud to 2?> class =user-link>全名1< / a>
< span class =user-subhead>成员< / span>
< / td>

< td>
< img src =http://bootdey.com/img/Content/user_1.jpgalt =>
< a href =<?php echo $ i; // shoud to be 3?> class =user-link>全名1< / a>
< span class =user-subhead>成员< / span>
< / td>

< td>
< img src =http://bootdey.com/img/Content/user_1.jpgalt =>
< a href =<?php echo $ i; // shoud to 4?> class =user-link>全名1< / a>
< span class =user-subhead>成员< / span>
< / td>

< / tr>

如果($ i%4 == 0){echo< tr>< / tr>;}


}?>


解决方案

的形式给出。
通常你有一个用户数组。
这个将创建一个4列的表,如果你有22个用户,只有20个用户会显示在这个例子中,因为它会限制输出来填满每一行。

  $ td = array(); 
$ userlist ='Bob','John','Robert','Eric','Lydia','Fanny','Alex','Leopold','Tom','Mark',' Bob2,John2,Robert2,Eric2,Lydia2,Fanny2,Alex2,Leopold2,Tom2,Mark2)
$ b $ 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.jpgalt =>
< a href ='。$ i。'class =user-link>全名。 $用户。 < / A>
< span class =user-subhead>成员< / span>';

}

echo'< table>< tr>'。 implode('< / tr>< tr>',$ td)。 < / 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>";}


} ?>

解决方案

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

这篇关于如何添加&lt; tr&gt;&lt; / tr&gt;在&lt; td&gt;&lt; / td&gt;的每第四个循环项之后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆