Create a Table For Each Database Row

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ReportsNerd
Forum Newbie
Posts: 4
Joined: Thu Jul 29, 2010 7:13 am

Create a Table For Each Database Row

Post by ReportsNerd »

I'm seeing a lot of examples out there of throwing each row of a query into a single table. I am looking for an example where each row from a database is displayed in its own table (vertically down the page). I want to create a member profile page with each members info in a nice formatted table. Help appreciated. Thanks in advance.

Frank
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Create a Table For Each Database Row

Post by jraede »

Just put the table HTML tags within the loop that goes through your query results. Something like

Code: Select all

<?php $results = mysql_query("YOUR QUERY"); 
while($row = mysql_fetch_assoc($results)) : ?>
<table>
    <tr>
         <td><?=$row['firstname']?>
         <td><?=$row['lastname']?>
    </tr>
</table>
<?php endwhile;?>
You get the idea.
emkay
Forum Newbie
Posts: 2
Joined: Thu Jul 29, 2010 6:40 am

Re: Create a Table For Each Database Row

Post by emkay »

This is very simple, you just do it the same way as you would represent it in a single table, but start and finish table tags in each loop, for example:

Code: Select all

//Database query
$q = "SELECT * FROM users";
//Execute query
$r = @mysqli_query($dbc,$q) OR die('Error obtaining user data: '.mysqli_error($dbc));
//Display data of each row returned within a table
while($user = mysqli_fetch_assoc($r)) {
    //Start table tag
    echo '<table>';
    //Content here
     
    //Close table tag
    echo '</table>';    
}
Post Reply