[英]Limit the Number of columns displayed in a Table from foreach loop
I have a Loop which is outputting data into a html form and table.我有一个循环,它将数据输出到 html 表单和表格中。 How can I limit the number of columns it displays, the reason i wanna do this is because i have limited space and the loop might run for 1000 times or even more depending on the user.
如何限制它显示的列数,我想这样做的原因是因为我的空间有限,循环可能会运行 1000 次甚至更多,具体取决于用户。
I want to Limit the Columns to say 6, so if the loop runs 6 times I want it to create a new row for another 6 columns and repeat again.我想将列限制为 6,所以如果循环运行 6 次,我希望它为另外 6 列创建一个新行并再次重复。
<form method="POST" action="script.php">
<table><tr>
<td><input type="checkbox" name="itemSelect[]" class="itemSelect" value="<?php echo $id; ?>" /></td>
<td>
<div class="item-box" data-id="<?php echo $id; ?>">
<img src="<?php echo $value['image_inventory']; ?>.png">
<div id="rarity">
<p> <?php
if (!isset($value['item_rarity'])) {
$rarity = "common";
echo $rarity;
} else {
$rarity = $value['item_rarity'];
echo $rarity;
}
?> </p>
</div>
</div></td>
</tr> </table>
<?php
}
}
?>
</table>
<button type="Submit">
Send Trade Offer
</button>
this should help, adjust the columns as needed:这应该会有所帮助,根据需要调整列:
<?php
$data = array_fill(0,100,"text");
$i = 0;
$columns = 8;
$rest = $columns-(count($data)%$columns);
?>
<table>
<tr>
<?php foreach($data as $cell): ?>
<?php if(!($i%$columns)) echo '</tr><tr>'?>
<td><?= $cell ?></td>
<?php $i++;?>
<?php endforeach;?>
<!-- fill rest cells-->
<?php if($rest<$columns):for($r=0;$r<$rest;$r++): ?>
<td>fill</td>
<?php endfor;endif; ?>
</tr>
</table>
PS- could be a better Idea to not use a table at all and just float each "cell" left (use a div element for example) that way the layout would be responsive PS- 可能是一个更好的主意,根本不使用表格,只向左浮动每个“单元格”(例如使用 div 元素),这样布局就会响应
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.