2015-11-05 45 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元素),這樣的佈局會有所反應