2015-03-19 60 views
0

我想用php动态创建一个html表格,并包含来自两个数组的数据。用foreach创建表而不重复表格标题

但是,我写的代码重复每行的表格标题(双关语不明)。

如何创建只有顶部表标题标签的表?

这里是我的代码:

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

} 

}

回答

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>"; 
+0

工作就像一个魅力!谢谢。 – Joel 2015-03-19 12:29:51