Displaying Data in Columns with PHP

Tutorial - Displaying Data in Columns with PHP

Here is a simple way of displaying data in columns in php. Right now we are going to do this with the most common data type or data structure in the programming world, the array. You can also apply this with grabbing data from a data table but for simplicity we will just stick to array data that is not from a data base.

Lets start with just throwing the whole code at your brain:

  1. <?php
  2. //tutorial - simple data columns
  3.  
  4. //data array...just US states
  5. $states = array("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Dist of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming");
  6.  
  7. //number of records
  8. $num_records = sizeof($states);
  9.  
  10. //max number of columns
  11. $max_cols = 2;
  12.  
  13. //find the number of records that should be in each column
  14. $records_per_col = ceil($num_records / $max_cols);
  15.  
  16. print '<strong>Displaying State Data in Columns</strong><br /><br />';
  17.  
  18. print '<table border = "1" width = "30%">
  19. <tr>
  20. <td valign = "top">
  21. <table border = "0">
  22. <tr>';
  23.  
  24. //start a $count variable
  25. //its here to match up with the $records_per_col for comparison as seen below
  26. //in the loop
  27. $count = 0;
  28.  
  29. foreach ($states as $state) {
  30.         //check if the current count is greater than the $records_per_col
  31.         //if so create new column
  32.         if ($count >= $records_per_col) {
  33.                 print '</tr></table></td><td valign = "top"><table border = "0"><tr>';
  34.                
  35.                 //set the count variable back to 0 or else there will be a ton of columns
  36.                 $count = 0;
  37.         } else {
  38.                 //if not start a new row for the next piece of data
  39.                 print '</tr><tr>';
  40.         }
  41.        
  42.         //print the data
  43.         print '<td>'.$state.'<td>';
  44.        
  45.         //increment the $count variable
  46.         $count++;
  47. }
  48.  
  49. print '</table>
  50. </td>
  51. </tr>
  52. </table>';
  53.  
  54. ?>

Now lets break it down:

This block right here holds the array data that holds all 50 states plus one (Dist of Columbia) in a variable $states. We also want to check to the number of records in that array data, set the number of max columns to display and calculate the number of records to display per column.

  1. <?php
  2. //data array...just US states
  3. $states = array("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Dist of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming");
  4.  
  5. //number of records
  6. $num_records = sizeof($states);
  7.  
  8. //max number of columns
  9. $max_cols = 2;
  10.  
  11. //find the number of records that should be in each column
  12. $records_per_col = ceil($num_records / $max_cols);
  13. ?>

In this next block we begin printing the html table display and initiate a variable called $count.

  1. <?php
  2. print '<strong>Displaying State Data in Columns</strong><br /><br />';
  3.  
  4. print '<table border = "1" width = "30%">
  5. <tr>
  6. <td valign = "top">
  7. <table border = "0">
  8. <tr>';
  9.  
  10. //start a $count variable
  11. //its here to match up with the $records_per_col for comparison as seen below
  12. //in the loop
  13. $count = 0;
  14. ?>

Now the real action with all this takes place in this block. We iterate through the $states array variable.

  1. foreach ($states as $state) {
  2.         //check if the current count is greater than the $records_per_col
  3.         //if so create new column
  4.         if ($count >= $records_per_col) {
  5.                 print '</tr></table></td><td valign = "top"><table border = "0"><tr>';
  6.                
  7.                 //set the count variable back to 0 or else there will be a ton of columns
  8.                 $count = 0;
  9.         } else {
  10.                 //if not start a new row for the next piece of data
  11.                 print '</tr><tr>';
  12.         }
  13.        
  14.         //print the data
  15.         print '<td>'.$state.'<td>';
  16.        
  17.         //increment the $count variable
  18.         $count++;
  19. }

Closing block. We end the columned table and the container table.

  1. <?php
  2. print '</table>
  3. </td>
  4. </tr>
  5. </table>';
  6. ?>

Again there are more complicated ways of doing this depending on how much data you are dealing with and what you are doing exactly.

Have fun,
-Rashaud Teague