2015-11-05 48 views
-1

我有一个循环,它将数据输出到html表格和表格。我如何限制它显示的列数,我之所以这样做是因为我的空间有限,循环可能会运行1000次甚至更多,具体取决于用户。限制从foreach循环表中显示的列数

我想将列限制为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> 

+0

你没有提供代码的重要部分(循环),我也无法完全理解你想要做什么。在我看来,你需要JavaScript,或隐藏> 6 – Enjoyted

+0

使用模数。你想在6列后结束行并添加一个新行?就像'if($ iRowCount%6 == 0)echo“”;'你的循环内部(带有行计数器)。 – CD001

回答

0

这应该帮助,根据需要调整列:

<?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-可能是一个更好的主意,在所有不使用一个表,静静地飘浮每一个“细胞”左(例如使用div元素),这样的布局会有所反应