【问题标题】:Create table with foreach without repeating table headings使用 foreach 创建表而不重复表标题
【发布时间】:2015-03-19 12:26:37
【问题描述】:

我想用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>";

}

}

【问题讨论】:

    标签: php arrays printing foreach echo


    【解决方案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>";
    

    【讨论】:

    • 工作就像一个魅力!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    相关资源
    最近更新 更多