我想用php动态创建一个html表,并包含来自两个数组的数据。
但是,我写的代码重复了每一行的表格标题(双关语没有意图)。
如何创建一个只在顶部有表标题标记的表?
这是我的代码:
foreach (array_combine($even, $odd) as $products => $numbers) {
echo "<table border='1'>";
echo "<tr>";
echo "<th>Product name</th>";
echo "<th>Sold</th>";
echo "</tr>";
echo "<tr>";
print("<td>" . ($products) . "</td>");
print("<td>" . ($numbers) . "</td>");
echo "</tr>";
echo "</table>";
}
}
答案 0 :(得分:1)
只需将表头放在foreach外观之外:
echo "<table border='1'>";
echo "<tr>";
echo "<th>Product name</th>";
echo "<th>Sold</th>";
echo "</tr>";
foreach (array_combine($even, $odd) as $products => $numbers) {
echo "<tr>";
print("<td>" . ($products) . "</td>");
print("<td>" . ($numbers) . "</td>");
echo "</tr>";
}
echo "</table>";