使用 foreach 创建 table 而不重复 table 标题
Create table with foreach without repeating table headings
我想用 php 动态创建 html table 并包含来自两个数组的数据。
但是,我编写的代码为每一行重复了 table 个标题(双关语无意)。
如何创建顶部只有 table 标题标签的 table?
这是我的代码:
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>";
}
}
只需将 table header 放在 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>";
我想用 php 动态创建 html table 并包含来自两个数组的数据。
但是,我编写的代码为每一行重复了 table 个标题(双关语无意)。
如何创建顶部只有 table 标题标签的 table?
这是我的代码:
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>";
}
}
只需将 table header 放在 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>";