正确地将php格式化为html表格

时间:2015-08-11 17:13:07

标签: php html-table

我有一个php数组,其中包含来自我的数据库$name[] = $row['name']的信息。容器emailagescreen-resolution还有大约3个其他行。

我试图整齐地将它组装成一个看起来像这样的表:

name {---------- {1}} {---------- {1}} ---------- {{ 1}}

email {--------- {1}} -------- age {--------- {1}} < / p>

screen-res {--------- {1}} -------- name1 {--------- {1}} < / p>

然而我的目前看起来像这样:

email1 {---------- {1}} {---------- {1}} ---------- {{ 1}}

age1 {--------- {1}} {---------- {1}} ------ res1 - ------- name2 ------ email2 ------- age2 ------ res2

name {--------- {1}} {---------- {1}} ------ email - ------- age ------ screen-res ------- name1

我的代码

name2

1 个答案:

答案 0 :(得分:2)

您正在错误地形成表格。你的桌子看起来像这样。好消息是您只需要一个数组来存储所有数据

<table>
  <tr>
    <th>Name</th>
    <th>Email</th>
    <th>Age</th>
    <th>Screen-Res</th>
  </tr>
  <?php

    foreach ($rows as $row) {
        echo "<tr>";
        echo "<td>".$row['nameVal']."</td>";

        echo "<td>".$row['emailVal']."</td>";

        echo "<td>".$row['ageVal']."</td>";

        echo "<td>".$row['screen-resVal']."</td>";
            echo "</tr>";
    }

  ?>
</table>