2016-03-16 51 views
0

我的表格有非循環的非特定長度的行,因爲可以隨時添加或刪除單元格中的值。總之,這裏的代碼:使用for循環在html表格中打印行

<?php 
    $i = 1; 
    foreach($items as $item => $itemValue) { 
    if ($itemValue['item_id'] == $parentItemValue['id']) { 
     if (fmod($i,7)) echo '<tr>'; 
     echo '<td class="inner-td"><input type="checkbox" id="itemId">'.$itemValue['item'].'</td>'; 
     if (!fmod($i,7)) echo '</tr>'; 
     $i++; 
    } 
?> 

上面的代碼顯示此: enter image description here

我也試過if (!fmod($i,7)) echo '<tr>'if (!fmod($i,8)) echo '</tr>',給我這樣的: enter image description here

此外,if (!fmod($i,10)) echo '<tr>'if (!fmod($i,11)) echo '</tr>',給我這個: enter image description here

我希望我的桌子看起來像這樣: enter image description here

有沒有一種方法可以讓單元格在填充整行之前填滿整行?

+1

IM不太清楚你真正想要的樣子 –

+0

你可以浮動了很多​​內的div跨越所有其他列。 –

+0

@Dagon編輯了這個問題。 –

回答

0

請嘗試遵循此結構。 請注意,所有字段和行都由我自己組成。

<tbody> 

     <?php 
     require_once ('connectionWithDB.php'); 
     $table = "members"; 
     $array = $admin_query->viewTableData($table); 
     foreach ($array as $row){ 
     print_r($array); 
     ?> 

     <tr> 
     <td> <?php $row[member_id] ?> </td> 
     <td> <?php $row[member_password] ?> </td> 
     <td> <?php $row[member_first_name] ?> </td> 
     <td> <?php $row[member_last_name] ?> </td> 
     <td> <?php $row[member_DOB] ?> </td> 
     <td> <?php $row[member_address] ?> </td> 
     <td> <?php $row[member_email] ?> </td> 
     <td> <?php $row[member_phone] ?> </td> 
     <td> <?php $row[member_gender] ?> </td> 
     </tr> 
     </tbody> 
+0

清理髒代碼,你應該嘗試限制你打開PHP服務器的頻率。 –

+0

對不起,這不是我正在尋找的人。無論如何,謝謝你的努力。 :) –

1

你可以試試這個。只需更改$maxcol值的多少列你想要的。

<?php 
$tmp = array('test1','test2','test3','test4'); 
echo '<table border="1">'; 
$x = 0; 
$maxcol = 2; // Max column 
foreach($tmp as $i=>$v) 
{ 

    echo $x === 0 ? '<tr>' : ''; 
    echo '<td>'.$v.'</td>'; 
    echo $x === ($maxcol-1) ? '</tr>' : ''; 
    $x++; 
    $x = $x == $maxcol ? 0 : $x; 

} 
echo '</table>'; 
?> 
+0

謝謝。但我已經在這裏得到了答案http://stackoverflow.com/questions/2156712/how-to-float-3-divs-side-by-side-using-css作爲我的一個問題評論人給出的例子上面, @acoder –

+0

好的。聽起來不錯。 – rmondesilva