-1

I want to show the list of an array in a table, I want to insert each ID in the ID column, each name in the "name" column, etc...

But it's showing all the content in single column, How I can fix it?

It shows like that right now:

[![enter image description here][1]][1]

PHP:

<table class="blueTable">
    <thead>
        <tr>
            <th>ID</th>
            <th>USER</th>
            <th>MAIL</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td colspan="3">
                <div class="links"><a href="#">&laquo;</a> <a class="active" href="#">1</a> <a href="#">2</a> <a href="#">3</a> <a href="#">4</a> <a href="#">&raquo;</a></div>
            </td>
        </tr>
    </tfoot>
    <tbody>
        <?php
$query = $db->query("SELECT * FROM users ORDER by id");
echo "<tr>";
while ($row = $query->fetch_array()) {
    echo "<td>" . $row['id'] . "</td>";
    echo "<tr><td>" . $row['username'] . "</td></tr>";
    echo "<tr><td>" . $row['email'] . "</td></tr>";
}
?>
 </tbody>
</table>



  [1]: https://i.stack.imgur.com/jPTR8.png
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98

2 Answers2

1

change while() loop code like below:

$query = $db->query("SELECT * FROM users ORDER by id");
while($row = $query->fetch_array()){
    echo "<tr>"; //put <tr> opening code inside, not outside
    echo "<td>".$row['id']."</td>";
    echo "<td>".$row['username']."</td>"; //remove <tr></tr>
    echo "<td>".$row['email']."</td>"; //remove <tr></tr>
    echo "</tr>"; //</tr> need to be added at last
}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
0

Try this

<?php
$query = $db->query("SELECT * FROM users ORDER by id");
while($row = $query->fetch_array()){
?>
<tr>
<td> <?php echo $row['id'] ?></td>
<td> <?php echo $row['username'] ?> </td>
<td> <?php echo $row['email'] ?>  </td>
</tr>
<?php
 }
?>
Reji kumar
  • 298
  • 3
  • 11