将 php 正确格式化为 html table
Formatting php into html table correctly
我有一个 php 数组,其中包含来自我的数据库 $name[] = $row['name']
的信息。还有大约 3 行容器 email
、age
和 screen-resolution
.
我正在尝试将其巧妙地组合成一个 table,看起来像:
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 格式不正确。你 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>
我有一个 php 数组,其中包含来自我的数据库 $name[] = $row['name']
的信息。还有大约 3 行容器 email
、age
和 screen-resolution
.
我正在尝试将其巧妙地组合成一个 table,看起来像:
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 格式不正确。你 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>