Ok, so first thing we’ll need to do is add some pre-output processing - in otherwords, first parse all of the information into an array. Then traverse the array checking if each field has values.
So, here’s what I came up with.
First have a list of possible field names and their table headers. Have an empty array which will contain those fields which are existent.
Loop through each record. For each field which has a value, that isn’t already in the current field list, add it to the field list and remove it from the fields to search. If all fields have already been found, there isn’t a reason to keep going through the records so break out of the loop.
Then loop through field names to output the table headers, and loop through each record, outputting cells for each field name.
Like so:
$MeasureArray = array();
$Columns = array(
'measurement_type' => 'Measurement Type',
'nose' => 'Nose',
'n6' => 'Nose 6"',
'n12' => 'Nose 12"',
'n24' => 'Nose 24"',
'n36' => 'Nose 36"',
'n48' => 'Nose 48"',
'center' => 'Center 12"',
't48' => 'Tail 48"',
't36' => 'Tail 36"',
't24' => 'Tail 24"',
't12' => 'Tail 12"',
't6' => 'Tail 6"',
'tail' => 'Tail"'
);
$ColumnsLeft = array_keys($Columns);
$FinalColumns = array();
foreach($measure as $m){
foreach($ColumnsLeft as $Key => $Value){
if($m->$Value != ""){
unset($ColumnsLeft[$Key]);
$FinalColumns[$Value] = $Columns[$Value];
}
}
if(empty($Columns)){
break;
}
}
echo '<table>';
echo '<tr>';
foreach($FinalColumns as $Name){
echo '<th>' . $Name . '</th>';
}
echo '</tr>';
foreach($measure as $m){
echo '<tr>';
foreach($FinalColumns as $Key => $Name){
echo '<td>';
echo $m->$Key;
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
Here’s the full code I used to test with, as I don’t have data in the database etc:
<?php
class Measure{
public $measurement_type = 'a', $nose, $n6, $n12, $n24, $n36, $n48, $center, $t48, $t36, $t24, $t12, $t6, $tail;
function __construct($nose, $n6, $n12, $n24, $n36, $n48, $center, $t48, $t36, $t24, $t12, $t6, $tail){
$this->nose = $nose;
$this->n6 = $n6;
$this->n12 = $n12;
$this->n24 = $n24;
$this->n36 = $n36;
$this->n48 = $n48;
$this->center = $center;
$this->t48 = $t48;
$this->t36 = $t36;
$this->t24 = $t24;
$this->t12 = $t12;
$this->t6 = $t6;
$this->tail = $tail;
}
}
$measure = array(
new Measure(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '', 12, 13),
new Measure(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '', 12, 13),
new Measure(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '', 12, 13),
new Measure(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '', 12, 13)
);
$MeasureArray = array();
$Columns = array(
'measurement_type' => 'Measurement Type',
'nose' => 'Nose',
'n6' => 'Nose 6"',
'n12' => 'Nose 12"',
'n24' => 'Nose 24"',
'n36' => 'Nose 36"',
'n48' => 'Nose 48"',
'center' => 'Center 12"',
't48' => 'Tail 48"',
't36' => 'Tail 36"',
't24' => 'Tail 24"',
't12' => 'Tail 12"',
't6' => 'Tail 6"',
'tail' => 'Tail"'
);
$FinalColumns = array();
foreach($measure as $m){
foreach($Columns as $Key => $Value){
if($m->$Key != ""){
unset($Columns[$Key]);
$FinalColumns[$Key] = $Value;
}
}
if(empty($Columns)){
break;
}
}
echo '<table>';
echo '<tr>';
foreach($FinalColumns as $Name){
echo '<th>' . $Name . '</th>';
}
echo '</tr>';
foreach($measure as $m){
echo '<tr>';
foreach($FinalColumns as $Key => $Name){
echo '<td>';
echo $m->$Key;
echo '</td>';
}
echo '</tr>';
}
echo '</table>';