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:
<?php
//tutorial - simple data columns
//data array...just US states
$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");
//number of records
$num_records = sizeof($states);
//max number of columns
$max_cols = 2;
//find the number of records that should be in each column
$records_per_col = ceil($num_records / $max_cols);
print '<strong>Displaying State Data in Columns</strong><br /><br />';
print '<table border = "1" width = "30%">
<tr>
<td valign = "top">
<table border = "0">
<tr>';
//start a $count variable
//its here to match up with the $records_per_col for comparison as seen below
//in the loop
$count = 0;
foreach ($states as $state) {
//check if the current count is greater than the $records_per_col
//if so create new column
if ($count >= $records_per_col) {
print '</tr></table></td><td valign = "top"><table border = "0"><tr>';
//set the count variable back to 0 or else there will be a ton of columns
$count = 0;
} else {
//if not start a new row for the next piece of data
}
//print the data
print '<td>'.$state.'<td>';
//increment the $count variable
$count++;
}
</td>
</tr>
</table>';
?>
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.
<?php
//data array...just US states
$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");
//number of records
$num_records = sizeof($states);
//max number of columns
$max_cols = 2;
//find the number of records that should be in each column
$records_per_col = ceil($num_records / $max_cols);
?>
In this next block we begin printing the html table display and initiate a variable called $count.
<?php
print '<strong>Displaying State Data in Columns</strong><br /><br />';
print '<table border = "1" width = "30%">
<tr>
<td valign = "top">
<table border = "0">
<tr>';
//start a $count variable
//its here to match up with the $records_per_col for comparison as seen below
//in the loop
$count = 0;
?>
Now the real action with all this takes place in this block. We iterate through the $states array variable.
foreach ($states as $state) {
//check if the current count is greater than the $records_per_col
//if so create new column
if ($count >= $records_per_col) {
print '</tr></table></td><td valign = "top"><table border = "0"><tr>';
//set the count variable back to 0 or else there will be a ton of columns
$count = 0;
} else {
//if not start a new row for the next piece of data
}
//print the data
print '<td>'.$state.'<td>';
//increment the $count variable
$count++;
}
Closing block. We end the columned table and the container table.
<?php
</td>
</tr>
</table>';
?>
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