格式化php到html表正确


Formatting php into html table correctly

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

我正试图将其整齐地组装成一个表,看起来像:

name ---------- email ---------- age ---------- screen-res

name1 --------- email1 -------- age1 --------- res1

name2 --------- email2 -------- age2 --------- res2

但是我现在看起来是这样的:

name ---------- email ---------- age ---------- screen-res

name1 --------- name2 ---------- name3——name4 --------- name5——name6——name7——name8

email1 --------- email2 ---------- email3——email --------- email5——email6 email7

我的代码

<table>
  <tr>
    <th>Name</th>
    <th>Email</th>
    <th>Age</th>
    <th>Screen-Res</th>
  </tr>
  <?php
    echo "<tr>";
    foreach ($name as $nameVal) {
        echo "<td>$nameVal</td>";
    }
    echo "</tr>";
    echo "<tr>";
    foreach ($email as $emailVal) {
        echo "<td>$emailVal</td>";
    }
    echo "</tr>";
    echo "<tr>";
    foreach ($age as $ageVal) {
        echo "<td>$ageVal</td>";
    }
    echo "</tr>";
    echo "<tr>";
    foreach ($screen-res as $screen-resVal) {
        echo "<td>$screen-resVal</td>";
    }
    echo "</tr>";
  ?>
</table>

您的表格格式不正确。你把桌子弄成这样。好处是你只需要一个数组来存放所有的数据

<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>