2015-03-19 39 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