如何添加< tr>< / tr>在< td>< / td>的每第四个循环项之后, [英] How to add <tr></tr> after every fourth loop term of <td></td>
问题描述
我想创建一个二十个人的列表,在每一个 tr
应该是4个人。
所以,我想每隔4 <循环次数。我的 我的代码如下: 的形式给出。 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 So, I want to break from the loop after every 4th number of loop. My one My code is below:
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.
这篇关于如何添加< tr>< / tr>在< td>< / td>的每第四个循环项之后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 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>< /表>;
tr
should to be 4 people.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. <?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>";}
} ?>
$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>';