我以前已經問過類似的問題,但我試圖將其適用於我的代碼,但它不能正常工作。PHP中的組織表
我有下面的代碼,在PHP中描繪了一個表,我想顯示PRODUCT
總當我發現PRODUCT
變化,我試圖做到以下幾點,但它讓我在每一個PRODUCT
開頭的TOTAL
行並且不TOTALIZE以及
變量GOLD
,NORMAL
是動態的,即有可能是沒有GOLD
沒有NORMAL
表,像其他人一樣:
這是我的代碼:
<?php
$harvest = Array (
Array (
Array (
'PRODUCT' => 'ROSE' ,
'VARIETY' => 'ADELE',
'GOLD' => 160,
'NORMAL' => 0 ,
'TOTAL' => 160
) ,
Array (
'PRODUCT' => 'ROSE',
'VARIETY' => 'ALESSO' ,
'GOLD' => 1320 ,
'NORMAL' => 550,
'TOTAL' => 1870
) ,
Array (
'PRODUCT' => 'ROSE',
'VARIETY' => 'ANASTACIA' ,
'GOLD' => 440 ,
'NORMAL' => 150 ,
'TOTAL' => 590
),
Array (
'PRODUCT' => 'ROSE1',
'VARIETY' => 'ANASTACIA1' ,
'GOLD' => 420 ,
'NORMAL' => 120 ,
'TOTAL' => 540
),
Array (
'PRODUCT' => 'ROSE1',
'VARIETY' => 'ANASTACIA1',
'GOLD' => 440 ,
'NORMAL' => 100 ,
'TOTAL' => 540
),
Array (
'PRODUCT' => 'ROSE2',
'VARIETY' => 'ANASTACIA2',
'GOLD' => 640,
'NORMAL' => 0,
'TOTAL' => 640
),
Array (
'PRODUCT' => 'ROSE2',
'VARIETY' => 'ANASTACIA2' ,
'GOLD' => 440,
'NORMAL' => 440,
'TOTAL' => 880
)
)
);
$arrayThead = array();
for ($i=0; $i < count($harvest) ; $i++) {
array_push($arrayThead, array_keys($harvest[$i][0]));
}
$totalByProduct = array();
foreach ($harvest as $items) {
foreach ($items as $item) {
if(!key_exists($item['PRODUCT'], $totalByProduct)){
$totalByProduct[$item['PRODUCT']] = $item;
continue;
}
foreach ($arrayThead as $key => $values) {
foreach ($values as $th) {
if($th != 'PRODUCT' && $th != 'VARIETY'){
$totalByProduct[$item['PRODUCT']][$th] += $item[$th];
}
}
}
}
}
$arrayfoot= array();
foreach ($harvest as $key => $value) {
foreach ($value as $harv) {
foreach ($arrayThead as $key => $values) {
foreach ($values as $th) {
if($th != 'PRODUCT' && $th != 'VARIETY'){
$arrayfoot[$th] += $harv[$th];
}
}
}
}
}
$arrayComplete = array();
for ($i=0; $i < count($arrayThead) ; $i++) {
for ($j=0; $j < count($arrayThead[$i]) ; $j++) {
if($arrayThead[$i][$j] != 'PRODUCT' && $arrayThead[$i][$j] != 'VARIETY'){
array_push($arrayComplete, $arrayThead[$i][$j]);
}
}
}
$arrayFinal = array();
for ($j=0; $j < count($arrayComplete) ; $j++) {
array_push($arrayFinal, $arrayfoot[$arrayComplete[$j]]);
}
$body = '<table style="border: 1px solid black;border-collapse: collapse;width: 100%;font-family:Calibri;">';
$body .= '<thead style="background-color:#f3f4f5;">';
$body .= '<tr>';
for ($i=0; $i < count($arrayThead) ; $i++) {
for ($j=0; $j < count($arrayThead[$i]) ; $j++) {
if($arrayThead[$i][$j] === 'PRODUCT' || $arrayThead[$i][$j] === 'VARIETY'){
$body .= '<th style="border: 1px solid black;height:50px;">'.$arrayThead[$i][$j].'</th>';
}else{
$body .= '<th style="border: 1px solid black;height:50px;">'.$arrayThead[$i][$j].'</th>';
}
}
}
$body .= '</tr>';
$body .= '</thead>';
$body .= '<tbody>';
//Initialize the variable product
$product = "";
foreach ($harvest as $items) {
foreach ($items as $item) {
//If the $product variable is different from the data product, print the total
if($product != $item['PRODUCT']){
$body .= '<tr>';
$body .= '<th style="border: 1px solid black;text-align:left;font-size:12px;">TOTAL '.$item['PRODUCT'].'</th>';
$body .= '<th style="border: 1px solid black;text-align:left;font-size:12px;"></th>';
foreach ($arrayThead as $key => $values) {
foreach ($values as $th) {
if($th != 'PRODUCT' && $th != 'VARIETY'){
$body .= '<th style="border: 1px solid black;text-align:right;font-size:12px;">'.number_format($total).'</th>';
}
}
}
$body .= '</tr>';
$product = $item['PRODUCT'];
}
$body .= '<tr>';
foreach ($arrayThead as $key => $values) {
foreach ($values as $th) {
if($th === 'PRODUCT' || $th === 'VARIETY'){
$body .= '<td style="border: 1px solid black;font-size:12px;">'.$item[$th].'</td>';
}else{
$body .= '<td style="border: 1px solid black; text-align:right;font-size:12px;">'.number_format($item[$th]).'</td>';
}
}
}
$body .= '</tr>';
$product = $item['PRODUCT'];
}
}
$body .= '</tbody>';
$body .= '<tfoot>';
$body .= '<tr>';
$body .= '<th style="border: 1px solid black;text-align:left;">TOTAL GENERAL</th>';
$body .= '<th style="border: 1px solid black;"></th>';
for ($i=0; $i < count($arrayFinal) ; $i++) {
$body .= '<th style="border: 1px solid black;text-align:right;">'.number_format($arrayFinal[$i]).'</th>';
}
$body .= '</tr>';
$body .= '</tfoot>';
$body .= '</table>';
echo $body;
要了解它的工作原理,請在以下網頁上覆制並粘貼代碼:
我希望有人能幫助我。
有什麼問題嗎?請減少代碼量以創建[最小化,完整和可驗證的示例](https://stackoverflow.com/help/mcve) – k0pernikus
您可以將代碼複製並粘貼到鏈接PHPTester中,您可以看到我的問題! @ k0pernikus –
你想得到社區的幫助。因此,請在問題中提供必要的信息,並且不要期望我們爲您完成工作。 – k0pernikus