Looping with PHP to Build a Table

Looping comes in handy when you may want to execute a certain script "x" number of times or if you want to display all the information within a Recordset. Here is an example of how I display all the information stored within a Recordset into a nice table that is easy to read.

<!-- setting up the table -->
<table border=”1″>
      <td>First Name</td>
      <td>Last Name</td>

Now that we have the initial table set up, we are ready to build the body.

//create the loop
do {
  print '<tr>';
  print '<td>' . $row_Recordset1['first_name'] . '</td>';
  print '<td>' . $row_Recordset1['last_name'] . '</td>';
  print '<td>' . $row_Recordset1['last_name'] . '</td>';
  print '</tr>';
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); 

Now that the body of the table is built, we close the table.


This will display all the information for Recordset1 within a table with the heading cells being First Name, Last Name, and ID. Recordset1 must first be established before you will be able to populate the cells or else no data is found.