2017-04-03 58 views
0

我以前已經問過類似的問題,但我試圖將其適用於我的代碼,但它不能正常工作。PHP中的組織表

我有下面的代碼,在PHP中描繪了一個表,我想顯示PRODUCT總當我發現PRODUCT變化,我試圖做到以下幾點,但它讓我在每一個PRODUCT開頭的TOTAL行並且不TOTALIZE以及

變量GOLDNORMAL是動態的,即有可能是沒有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; 

要了解它的工作原理,請在以下網頁上覆制並粘貼代碼:

PHPTester

我希望有人能幫助我。

+0

有什麼問題嗎?請減少代碼量以創建[最小化,完整和可驗證的示例](https://stackoverflow.com/help/mcve) – k0pernikus

+0

您可以將代碼複製並粘貼到鏈接PHPTester中,您可以看到我的問題! @ k0pernikus –

+0

你想得到社區的幫助。因此,請在問題中提供必要的信息,並且不要期望我們爲您完成工作。 – k0pernikus

回答

0

可能是你正在尋找這樣的事情

<? 
$harvest = 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 
        ) 

); 


//You will endup with a products array indexed by product name 
$products = []; 

$general = Array('GOLD' => 0,'NORMAL' => 0, 'TOTAL' => 0); 
foreach ($harvest as $items) { 
    $general["GOLD"] += $items["GOLD"]; 
    $general["NORMAL"] += $items["NORMAL"]; 
    $general["TOTAL"] += $items["TOTAL"]; 
    if (array_key_exists($items["PRODUCT"],$products)){ 
     $products[$items["PRODUCT"]]["TOTALS"]["GOLD-TOTAL"] += $items["GOLD"]; 
     $products[$items["PRODUCT"]]["TOTALS"]["NORMAL-TOTAL"] += $items["NORMAL"]; 
     $products[$items["PRODUCT"]]["TOTALS"]["TOTAL"] += $items["TOTAL"]; 
     array_push($products[$items["PRODUCT"]],$items); 
    } 
    else{ 
     $totals=[]; 
     $totals["GOLD-TOTAL"] = 0; 
     $totals["NORMAL-TOTAL"] = 0; 
     $totals["TOTAL"] = 0; 
     $totals["NAME"] = $items["PRODUCT"]; 
     $products[$items["PRODUCT"]]=[$items]; 
     $products[$items["PRODUCT"]]["TOTALS"] = $totals; 
    } 
} 

?>

<table> 
<thead> 
    <th>PRODUCT</th> 
    <th>VARIETY</th> 
    <th>GOLD</th> 
    <th>NORMAL</th> 
    <th>TOTAL</th> 
</thead> 
<tbody> 
    <?foreach ($products as $product) { 
     foreach ($product as $item){ 
      if (array_key_exists("PRODUCT",$item)){ 
    ?> 

    <tr> 
     <td><?print($item["PRODUCT"])?></td> 
     <td><?print($item["VARIETY"])?></td> 
     <td><?print($item["GOLD"])?></td> 
     <td><?print($item["NORMAL"])?></td> 
     <td><?print($item["TOTAL"])?></td> 
    </tr> 
    <?}}?> 
    <tr> 
     <td>Total <? print($product["TOTALS"]["NAME"])?></td> 
     <td></td> 
     <td><? print($product["TOTALS"]["GOLD-TOTAL"])?></td> 
     <td><? print($product["TOTALS"]["NORMAL-TOTAL"])?></td> 
     <td><? print($product["TOTALS"]["TOTAL"])?></td> 
    </tr> 
    <?}?> 
    <tr> 
     <td>TOTAL GENERAL</td> 
     <td></td> 
     <td><?print($general["GOLD"])?></td> 
     <td><?print($general["NORMAL"])?></td> 
     <td><?print($general["TOTAL"])?></td> 
    </tr> 
</tbody> 
</table> 
+0

您是否在此頁面中嘗試過? PHPTester http://phptester.net/ –

+0

我試試這個,而不是我的代碼http://sandbox.onlinephpfunctions.com/ –

+0

好吧我現在檢查你的代碼,我知道你在問什麼。我將編輯我的答案 –