Add rows to table even if not in array
Not sure how to title this, so I apologize if it's misleading or not understandable. What I have is an array, that array will have 1-4 arrays within it. I need a HTML table to output 4 columns开发者_JAVA百科, so even if the array only has one array in it, it still needs to output for columns. The issue I am hitting is that the columns need to match up. Inside of the arrays there is a key called 'Deductible' and that key matches up with the HTML table's columns. The Array, PHP, current HTML output and wanted HTML output are all below and a link to PasteBin in case this post doesn't display it all correctly.
// OUTPUT OF $estimateOutput
array (
0 =>
array (
'AdminCode' => 'NASC',
'AdminName' =>
array (
),
'NewUsed' => 'Used',
'CoverageCode' => 'NCGUGOLD',
'CoverageName' => 'GOLD USED COMPONENT 1-10',
'GenericCoverage' =>
array (
),
'Term' => '24/24',
'TermInMonths' => '24',
'TermInMilesKM' => '24000',
'Deductible' => '100',
'RetailCost' => '1377.0',
),
1 =>
array (
'AdminCode' => 'NASC',
'AdminName' =>
array (
),
'NewUsed' => 'Used',
'CoverageCode' => 'NCGUGOLD',
'CoverageName' => 'GOLD USED COMPONENT 1-10',
'GenericCoverage' =>
array (
),
'Term' => '24/24',
'TermInMonths' => '24',
'TermInMilesKM' => '24000',
'Deductible' => '50',
'RetailCost' => '1462.0',
),
)
$estimateOutput .= '<tr class="month-section"><td colspan="5"><strong>'.$term_length.' Months</strong></td></tr>';
if($rateEstimate != ""){
foreach($rateEstimate as $rate) {
$ratesByMiles[$rate["TermInMilesKM"]][] = $rate;
}
foreach($rateEstimate as $key=>$value){
if($TermInMilesKM != $value['TermInMilesKM']){
$estimateOutput .= '<tr>';
$estimateOutput .= '<td>'.number_format($value['TermInMilesKM']).'</td>';
foreach($ratesByMiles[$value['TermInMilesKM']] as $newval){
$estimateOutput .= '<td>';
$estimateOutput .= '<a href="#" rel="'.$newval['CoverageCode'].'::'.$newval['Term'].'::'.$newval['Deductible'].'::'.$newval['RetailCost'].'">$';
$estimateOutput .= number_format($newval['RetailCost']);
$estimateOutput .= '</a>';
$estimateOutput .= '</td>';
}
$estimateOutput .= '</tr>';
$TermInMilesKM = $value['TermInMilesKM'];
}
}
} else {
$estimateOutput .= '<tr><td colspan="5">Not Available</td></tr>';
}
echo $estimateOutput;
// CURRENTLY OUTPUTTING
<tr class="month-section" style="display: table-row;">
<td colspan="5"><strong>24 Months</strong></td>
</tr>
<tr style="display: table-row;">
<td>24,000</td>
<td><a rel="NCGUGOLD::24/24::50::1462.0" href="#">$1,462</a></td>
<td><a rel="NCGUGOLD::24/24::100::1377.0" href="#">$1,377</a></td>
</tr>
// NEED IT TO OUTPUT
<tr class="month-section" style="display: table-row;">
<td colspan="5"><strong>24 Months</strong></td>
</tr>
<tr style="display: table-row;">
<td>24,000</td>
<td>--</td> // If $newval['Deductible'] == 0 this should show data, otherwise --
<td><a rel="NCGUGOLD::24/24::50::1462.0" href="#">$1,462</a></td> // If $newval['Deductible'] == 50 this should show data, otherwise --
<td><a rel="NCGUGOLD::24/24::100::1377.0" href="#">$1,377</a></td> // If $newval['Deductible'] == 100 this should show data, otherwise --
<td>--</td> // If $newval['Deductible'] == 200 this should show data, otherwise --
</tr>
http://pastebin.com/y41MA8VZ
Always loop four times, then find the correct array to print:
$deductable_values = array(0, 50, 100, 200);
foreach ($deductable_values as $deductable_value) {
$data = find_deductible_array($estimateOutput, $deductable_value);
// output a row using $data, which may be null.
}
function find_deductible_array($estimateOutput, $value) {
foreach ($estimateOutput as $row) {
if ($row['deductible'] == $value) {
return $row;
}
}
// not found
return null;
}
精彩评论