Page 1 of 1

Create a Table For Each Database Row

Posted: Thu Jul 29, 2010 7:26 am
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

Re: Create a Table For Each Database Row

Posted: Thu Jul 29, 2010 8:55 am
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.

Re: Create a Table For Each Database Row

Posted: Thu Jul 29, 2010 8:57 am
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>';    
}