2017-04-03 36 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

好吧我现在检查你的代码,我知道你在问什么。我将编辑我的答案 –