2016-03-16 47 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