user13261747
user13261747

Reputation:

Array to HTML Table

I try to insert data from my php array into an html table. The code for this looks like

echo "<table style='border: 1px solid black'><tbody>";
foreach(['country','counter'] as $attribute){
    echo "<tr><td>".$attribute."</td>";
    foreach($analysis_data as $row){
        echo "<td style='border: 1px solid black'>".$row[$attribute]."<td>";
    }
    echo '</tr>';

When the code is executed it looks like current solution

I want the table to be vertical and not horizontal like this. What do I need to change in my code?

Upvotes: 0

Views: 48

Answers (1)

Barmar
Barmar

Reputation: 781004

Interchange your two loops.

echo "<table style='border: 1px solid black'><tbody>";
echo "<tr><th>country</th><th>counter</th></tr>";
for ($analysis_data as $row) {
    echo '<tr>';
    foreach (['country','counter'] as $attribute){
        echo "<td style='border: 1px solid black'>".$row[$attribute]."<td>";
    }
    echo '</tr>';
}

Upvotes: 1

Related Questions