2012-01-04 66 views
-3

我必须用mysql结果填充html表。我希望表格为四列,并将结果排序在每个单元格中。在具有流布局的HTML表中显示来自MySQL查询的结果

从MySQL的结果是:

----------------- 
| Number | Type | 
----------------- 
| 1 | A | 
| 2 | B | 
| 3 | C | 
| 4 | D | 
| 5 | E | 
| 6 | F | 
----------------- 

而且我想HTML表看起来像这样:

----------------- 
| A | B | C | D | 
----------------- 
| E | F | | | 
----------------- 

我使用PHP来处理MySQL的查询。

+1

你在哪里卡住了? – 2012-01-04 11:45:42

+0

这里你会得到帮助,当你坚持做某事,但你似乎想要完整的代码>> – 2012-01-04 11:49:20

+0

当试图做'for'或'while'来生成html表并将结果分成四个不同的结果行。 @juergend @jogesh_p – estevecp96 2012-01-04 11:49:51

回答

1
<?php 
$con=mysql_connect(...); 
$qry=mysql_query("select Type from tablename "); 
echo "<table>"; 
$more=true; 
while ($more) { 
    echo "<tr>"; 
    for ($i=0;$i<4;$i++) { 
    $fld=mysql_fetch_row($qry); 
    if ($fld) $html=$fld[0]; 
    else { 
     $html='&nbsp;' 
     $more=false; 
    echo "<td>$html</td>"; 
    } 
    echo "</tr>"; 
} 
echo "</table>"; 
?> 
+0

非常感谢。欣赏你的答案。 @eugenrieck – estevecp96 2012-01-04 11:55:15

+0

2维循环?.. – 2012-01-04 12:01:02

+0

二维循环的二维问题 – 2012-01-04 12:08:26

0
$out = "<table><tr>"; 
    for(i=0;i<=$max_rows;$i++){ 
     $out .= ($i%4 ? "" : "</tr>".($i==$max_rows-1 ? "" : "<tr>"))."<td>".$result[$i]."</td>"; 
    } 
$out .= "</table>"; 
+0

也感谢你的回答 – estevecp96 2012-01-04 23:22:05

0

从数据库中首先提取数据:

$q = mysql_query("SELECT letters from TABLE"); 
while($fetch = mysql_fetch_assoc($q)) $data[] = $fetch['letters']; 

则有四个coloumns创建表:

<table> 
    <tr> 
    <?php $x = 0; for($i=0;$i<count($data);$i++) : ?> 
     <td><?php echo $data[$i]; ?></td> 
     <? $x++; ?> 
     <? if($x==4) : ?> 
      <? $x = 0; ?> 
      </tr><tr> 
     <? endif; ?> 
    <? endfor; ?> 
</tr> 
</table> 
+0

也谢谢你的回答 – estevecp96 2012-01-04 23:26:05