Mr.Smithyyy
Mr.Smithyyy

Reputation: 1329

Formatting php into html table correctly

I have a php array that contains information from my database $name[] = $row['name']. There are also about 3 other rows that container email, age, and screen-resolution.

I am trying to neatly assemle this into a table that looks like:

name----------email----------age----------screen-res

name1---------email1--------age1---------res1

name2---------email2--------age2---------res2

However mine currently looks like this:

name----------email----------age----------screen-res

name1---------name2----------name3------name4---------name5------name6-------name7------name8

email1---------email2----------email3------email---------email5------email6-------email7

My Code

<table>
  <tr>
    <th>Name</th>
    <th>Email</th>
    <th>Age</th>
    <th>Screen-Res</th>
  </tr>
  <?php
    echo "<tr>";
    foreach ($name as $nameVal) {
        echo "<td>$nameVal</td>";
    }
    echo "</tr>";

    echo "<tr>";
    foreach ($email as $emailVal) {
        echo "<td>$emailVal</td>";
    }
    echo "</tr>";

    echo "<tr>";
    foreach ($age as $ageVal) {
        echo "<td>$ageVal</td>";
    }
    echo "</tr>";

    echo "<tr>";
    foreach ($screen-res as $screen-resVal) {
        echo "<td>$screen-resVal</td>";
    }
    echo "</tr>";
  ?>
</table>

Upvotes: 0

Views: 89

Answers (1)

Daniel Payne
Daniel Payne

Reputation: 125

You are forming your tables incorrectly. You your table to look something like this. The good thing is you only need one array for all your data

<table>
  <tr>
    <th>Name</th>
    <th>Email</th>
    <th>Age</th>
    <th>Screen-Res</th>
  </tr>
  <?php

    foreach ($rows as $row) {
        echo "<tr>";
        echo "<td>".$row['nameVal']."</td>";

        echo "<td>".$row['emailVal']."</td>";

        echo "<td>".$row['ageVal']."</td>";

        echo "<td>".$row['screen-resVal']."</td>";
            echo "</tr>";
    }

  ?>
</table>

Upvotes: 2

Related Questions