0

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?

4
  • What have you tried already (that isn't working as expected)? Commented Jul 21, 2020 at 5:58
  • @kerbh0lz He posted his code that doesn't work. Commented Jul 21, 2020 at 6:01
  • @Barmar I mean there doesn't seem to be any attempt to code a vertical table as OP wants. Nevermind Commented Jul 21, 2020 at 6:09
  • @kerbh0lz He thought he was doing it, but he it didn't work as expected. Commented Jul 21, 2020 at 6:11

1 Answer 1

1

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>';
}
Sign up to request clarification or add additional context in comments.

Comments