Jump to content

Looping to create a table


RynMan

Recommended Posts

Hey guys

 

First off, I'm a php rookie, so go easy!

 

Basically, what I'm trying to do is this....

 

1.  Call on a query with several hundred records.

2.  Display 2 of these fields (FirstName & LastName) for each record

3.  Use PHP to create a new row for every 6 records until all records are done

 

So the finished product will be a table with all of the people's names - with 6 columns, and many rows (record 1 in the first column, record 2 in the second column, 3 in the third etc).

 

I know how to call my database, perform the query I want to pull the information from.  I just don't know how to set up this loop.

 

I need the PHP to basically say:

 

- while there are still records, perform this task

- for each 6 records, create a cell, then move to the next line and start creating a new row with the next 6 records.

 

Thanks for any help you can lend me guys!!

 

 

Link to comment
https://forums.phpfreaks.com/topic/156623-looping-to-create-a-table/
Share on other sites

Something like this:

$table = '<table><tr>';
$count = 0;
while ($row = mysql_fetch_assoc($result)) {
     if (!empty($count) && $count % 6 == 0) {
          $table .= '</tr><tr>';
     }
     $table .= '<td></td>';  // put your contents here
}
$table .= '</tr></table>';

 

Probably not the best way.

Something like this:

$table = '<table><tr>';
$count = 0;
while ($row = mysql_fetch_assoc($result)) {
     if (!empty($count) && $count % 6 == 0) {
          $table .= '</tr><tr>';
     }
     $table .= '<td></td>';  // put your contents here
}
$table .= '</tr></table>';

 

Probably not the best way.

 

Thanks ken.

 

If I put my contents in there....

 

$table .= '<td></td>'; 

 

What I actually want is $row.["FirstName"]

 

Will this not just repeat the same first name six times? 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.