表,TR每2个循环,PHP,HTML [英] Table, TR each 2 loop, PHP, HTML
问题描述
我得到一个html表格,我用一些循环来获取一些数据, < TR>< TD>数据< / TD>< / TR>
... next loop
但我不想关闭表格行(tr )每2或3个循环。因此,数据可能如下所示:
< tr>
< td>资料< / td>
< td> Data1< / td>
< td> Data2< / td>
< / tr>
...下一个循环...
你能帮我吗这是什么?
如果你的循环中有一些计数器,你可以使用Modulus。
基本上,如果您将它分开,那么它就剩下了一个数字。
示例:
<$ ($ i%2 === 0){
print(')是$($ i = 1; $ i <11; $ i ++){
>这是每两次印刷');
}
if($ i%3 === 0){
print('每3次打印一次');
$ b $如果你使用一个 foreach ()
代替,你应该自己创建一个计数器(如Link所述,如果数组中包含好的增量键,也可以使用数组的键
):
$ i = 1;
foreach($ array as $ item){
if($ i%2 === 0){
print('this is printed two times');
}
if($ i%3 === 0){
print('每3次打印一次');
}
$ i ++;
}
或者在您的具体情况下,它看起来像这样:
print('< tr>');
$ i = 1;
foreach($ array as $ item){
if($ i%3 === 0){
print(< / tr> \\\
< tr>);
}
print(< td> $ item< / td> \\\
);
$ i ++;
}
print('< / tr>');
以上只是一个基本的例子。
你还应该检查列的数量是否平衡,如果不添加一个colspan或一个空列来平衡它。
I got an html table, and I use some loop to get some data, this data is displaying this way:
<tr><td>Data</td></tr>
... next loop
But I wan't it to close table row (tr) every 2 or even 3 loops. So the data may look like this:
<tr>
<td>Data</td>
<td>Data1</td>
<td>Data2</td>
</tr>
...next loop...
Will you help me with this?
If you have some counter in your loop you can use Modulus for this.
It's basically what's left of a number if you divide it.
Example:
for($i = 1; $i < 11; $i++) {
if ($i % 2 === 0) {
print('this is printed every two times');
}
if ($i % 3 === 0) {
print('this is printed every three times');
}
}
If you use a foreach()
in stead you should just make a counter yourself (as Link stated you could also use the key
of an array if it contains nice incremental keys):
$i = 1;
foreach($array as $item) {
if ($i % 2 === 0) {
print('this is printed every two times');
}
if ($i % 3 === 0) {
print('this is printed every three times');
}
$i++;
}
Or in your specific case it would look something like:
print('<tr>');
$i = 1;
foreach($array as $item) {
if ($i % 3 === 0) {
print("</tr>\n<tr>");
}
print("<td>$item</td>\n");
$i++;
}
print('</tr>');
The above is just a basic example.
You should also check whether the number of the columns is balanced and if not either add a colspan or an empty columns to balance it.
这篇关于表,TR每2个循环,PHP,HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!