简体   繁体   中英

Create table with foreach without repeating table headings

I want to create a html table dynamically with php and include data from two arrays.

However, the code I have written repeats the table headings for each row (pun not intented).

How can I create a table that only has the table heading tags at the top?

Here is my code:

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

}

}

Just put the table header out of your foreach look:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM