Looping with PHP to Build a Table

Error message

Deprecated function: Function create_function() is deprecated in GeSHi->_optimize_regexp_list_tokens_to_string() (line 4698 of /home/digipiph/public_html/sites/all/libraries/geshi/geshi.php).

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″>
  <thead>  
    <tr>
      <td>First Name</td>
      <td>Last Name</td>
      <td>Id</td>
    </tr>
  </thead>
  <tbody>

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

<?php
//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.

  </tbody>
</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.

Tags: