根据PHP中的参数将总计添加到表中

时间:2017-03-31 19:43:05

标签: php arrays

我有这个代码在PHP中显示一个表:

<?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;
                  unset($totalByProduct[$item['PRODUCT']]['VARIETY']);
                  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];
              }
            }
          }
        }
      }

      $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]]);
      }

    //  exit;

      $body = '';
      $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>';

      for ($k=0; $k < count($harvest); $k++) {
        for ($a=0; $a < count($harvest[$k]) ; $a++) {
          $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 .= '<td style="border: 1px solid black;font-size:12px;">'.$harvest[$k][$a][$arrayThead[$i][$j]].'</td>';
              }else{
                $body .= '<td style="border: 1px solid black; text-align:right;font-size:12px;">'.number_format($harvest[$k][$a][$arrayThead[$i][$j]]).'</td>';
              }
            }
          }
          $body .= '</tr>';
          foreach ($totalByProduct as $key => $value) {
            if($value["PRODUCT"] == $harvest[$k][$a]['PRODUCT']){
              $body .= '<tr>';
              $body .= '<th style="border: 1px solid black;text-align:left;font-size:12px;">TOTAL '.$value["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($value[$th]).'</th>';
                  }
                }
              }
              $body .= '</tr>';
            }
          }
        }
      }

      $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;

我需要在表格中放置一行GOLD,NORMAL和TOTAL,但是根据产品。

如果您想了解代码的工作原理,可以在此页面中复制代码和过去:PHPTESTER

我需要向表格显示如下内容:

-------------------------------------------------------------------
  PRODUCT     |   VARIETY   |  GOLD    |  NORMAL |  TOTAL         |  
-------------------------------------------------------------------
   ROSE       |   ADELE     |  160     |  0      |     160        |
-------------------------------------------------------------------
   ROSE       |   ALESSO    |  1320    |  550    |    1870        |
-------------------------------------------------------------------
   ROSE       |  ANASTACIA  |  440     |    150  |  590           |
-------------------------------------------------------------------
   TOTAL ROSE |             |  1920    |  700    |  2620          |
-------------------------------------------------------------------
   ROSE1      |  ANASTACIA1 |  420     |  120    |  540           |
-------------------------------------------------------------------
   ROSE1      |  ANASTACIA1 |  440     |    100  |  540           |
-------------------------------------------------------------------
 TOTAL ROSE1  |             |  860     |  220    |  1080          |
-------------------------------------------------------------------
   ROSE2      |  ANASTACIA2 |  640     |    0    |  640           |
-------------------------------------------------------------------
   ROSE2      |  ANASTACIA2 |  440     |    440  |  880           |
-------------------------------------------------------------------
  TOTAL ROSE2 |             |  1080    |  440    |  1520          |
-------------------------------------------------------------------
 TOTAL GENERAL|             |  3860    |  1360   |  5220          |
-------------------------------------------------------------------

我希望有人可以帮助我谢谢!

2 个答案:

答案 0 :(得分:0)

这是代码。您可以修改代码

foreach($harvest[0] as $key=>$value){
    $new_array[$value['PRODUCT']][] = $value;
}

echo '<table cellspacing="5" cellpadding="5" border="1px solid #ccc">';
echo "<tr>
<th>PRODUCT</th>
<th>VARIETY</th>
<th>GOLD</th>
<th>NORMAL</th>
<th>TOTAL</th>
</tr>";



$total_gold = $total_normal = $total_total = 0;
foreach($new_array as $key=>$value){
    $gold[$key] = $normal[$key] = $total[$key] = 0;
    if(is_array($value)){
        foreach($value as $k=>$v){
            $gold[$key] += $v['GOLD'];
            $normal[$key] += $v['NORMAL'];
            $total[$key] += $v['TOTAL'];
            echo "<tr>";
            echo "<td>".$key."</td>";
            echo "<td>".$v['VARIETY']."</td>";
            echo "<td>".$v['GOLD']."</td>";
            echo "<td>".$v['NORMAL']."</td>";
            echo "<td>".$v['TOTAL']."</td>";
            echo "<tr>";
        }
        echo "<tr><td><b>Total ".$key."</b></td><td></td><td>".$gold[$key]."</td><td>".$normal[$key]."</td><td>".$total[$key]."</td></tr>";
        $total_gold +=$gold[$key];
        $total_normal +=$normal[$key];
        $total_total += $total[$key];
    }

}
echo "<tr><td><b>TOTAL GENERAL</b></td><td></td><td>".$total_gold."</td><td>".$total_normal."</td><td>".$total_total."</td></tr>";

echo "</table>";

答案 1 :(得分:0)

<?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 
                                   )
                    )


     );

$prods = array();
$prevprod = "";
$total_gold = 0;
$total_normal = 0;
$total_prod = 0;
$total = 0;

foreach($harvest[0] as $key => $val){

    $pos = $key;
    $obj = $val;

    $total_prod = $obj["GOLD"]+$obj["NORMAL"];
    if($obj["PRODUCT"]==$prevprod || $prevprod==""){
        $prods[] = array($obj["PRODUCT"],$obj["VARIETY"],$obj["GOLD"],$obj["NORMAL"],$obj["TOTAL"]);
        $total_gold = $total_gold + $obj["GOLD"];
        $total_normal = $total_normal + $obj["NORMAL"];
        $total_prod = $total_prod + $obj["TOTAL"];
        $total = $total + $total_prod;
    }else{
        $prods[] = array( "TOTAL ".$prevprod, "", $total_gold,$total_normal,$total_prod);
        $total_gold = 0;
        $total_normal = 0;
        $total_prod = 0;
    }

    $prevprod = $obj["PRODUCT"];
}
$prods[] = array( "TOTAL ".$prevprod, "", $total_gold,$total_normal,$total_prod);



    //  exit;

      $body = '';
      $body .= '<table style="border: 1px solid black;border-collapse: collapse;width: 100%;font-family:Calibri;">';
      $body .= '<thead style="background-color:#f3f4f5;">';
      $body .= '<tr>';

      $body .= '<th>PRODUCT</th><th>VARIETY</th><th>GOLD</th><th>NORMAL</th><th>TOTAL</th>';
      $body .= '</tr>';
      $body .= '</thead>';
      $body .= '<tbody>';

      foreach($prods as $p){
        $body .= '<tr><td>'.$p[0].'</td><td>'.$p[1].'</td><td>'.$p[2].'</td><td>'.$p[3].'</td><td>'.$p[4].'</td><tr>';
      }

      $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><th style="border: 1px solid black;"></th><th style="border: 1px solid black;"></th>';

      $body .= '<th>'.$total.'</th>';
      $body .= '</tr>';
      $body .= '</tfoot>';
      $body .= '</table>';

      echo $body;