0

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>

1 Answer 1

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.