2016-09-28 55 views
1

我有一个数组,我已经显示到表像波纹管:PHP条件行计算

+-------+-------+-------------------+ 
|  | Sales | Earned by date ($)| 
| Name | Code |-------+-----+-----+ 
|  |  | 1 | 2 | 3 |... end of month 
+-------+-------+-------+-----+-----+ 
| Jhon | A | 4.5 | 7 | 2 | 
| Jhon | B | 1.5 | 7 | 5 | 
| Jhon | C | 8.2 | 4 | 4 | 
| Ryan | A | 4 | 6 | 3 | 
| Ryan | B | 5 | 7 | 2 | 
| Ryan | C | 2.9 | 9 | 7 | 
| Jeny | A | 9.1 | 3 | 4 | 
| Jeny | B | 5 | 5 | 6 | 
| Jeny | C | 7 | 6 | 3 | 
+-------+-------+-------+-----+-----+ 

在表格的最后一排,我想,以填补基础上,在每个日期的总行值包含相同销售代码的行的摘要。

我试过的语法如下:

<?php 
$data = array(
    "0" => array("name" => "Jhon", "sales_code"=>"A", "1" => 4.5, "2"=>7, "3"=>2), 
    "1" => array("name" => "Jhon", "sales_code"=>"B", "1" => 1.5, "2"=>7, "3"=>5), 
    "2" => array("name" => "Jhon", "sales_code"=>"C", "1" => 8.2, "2"=>4, "3"=>4), 
    "3" => array("name" => "Ryan", "sales_code"=>"A", "1" => 4, "2"=>6, "3"=>3), 
    "4" => array("name" => "Ryan", "sales_code"=>"B", "1" => 5, "2"=>7, "3"=>2), 
    "5" => array("name" => "Ryan", "sales_code"=>"C", "1" => 2.9, "2"=>9, "3"=>7), 
    "6" => array("name" => "Jeny", "sales_code"=>"A", "1" => 9.1, "2"=>3, "3"=>4), 
    "7" => array("name" => "Jeny", "sales_code"=>"B", "1" => 5, "2"=>5, "3"=>6), 
    "8" => array("name" => "Jeny", "sales_code"=>"C", "1" => 7, "2"=>6, "3"=>3), 
    ); 


    ?> 
    <table border="1" width=""> 
     <tr> 
      <th rowspan="2">Name</th> 
      <th rowspan="2">Sales code</th> 
      <th colspan="3">Earned by date ($)</th> 
     </tr> 
     <tr> 
      <th>1</th> 
      <th>2</th> 
      <th>3</th> 
     </tr> 
     <?php 
     $a_tot = 0; 
     $c_tot = 0; 
     $b_tot = 0; 
     foreach($data as $row){ 
      ?> 
      <tr> 
       <td><?=$row['name']?></td> 
       <td><?=$row['sales_code']?></td> 
       <td><?=$row['1']?></td> 
       <td><?=$row['2']?></td> 
       <td><?=$row['3']?></td> 
      </tr> 
      <?php 
      $a_tot += ($row['sales_code'] == "A" ? $row['1'] : 0); 
      $b_tot += ($row['sales_code'] == "B" ? $row['2'] : 0); 
      $c_tot += ($row['sales_code'] == "C" ? $row['3'] : 0); 
     } 
     ?> 
     <tr> 
      <td>TOTAL</td><td>A</td> <td><?=$a_tot?></td> <td><?=$a_tot?></td> <td><?=$a_tot?></td> 
     </tr> 
     <tr> 
      <td>TOTAL</td><td>B</td> <td><?=$b_tot?></td> <td><?=$b_tot?></td> <td><?=$b_tot?></td> 
     </tr> 
     <tr> 
      <td>TOTAL</td><td>C</td> <td><?=$c_tot?></td> <td><?=$c_tot?></td> <td><?=$c_tot?></td> 
     </tr> 

    </table> 

但结果却弄成这个样子:

+-------+-------+-------------------+ 
|  | Sales | Earned by date ($)| 
| Name | Code |-------+-----+-----+ 
|  |  | 1 | 2 | 3 | 
+-------+-------+-------+-----+-----+ 
| Jhon | A | 4.5 | 7 | 2 | 
| Jhon | B | 1.5 | 7 | 5 | 
| Jhon | C | 8.2 | 4 | 4 | 
| Ryan | A | 4 | 6 | 3 | 
| Ryan | B | 5 | 7 | 2 | 
| Ryan | C | 2.9 | 9 | 7 | 
| Jeny | A | 9.1 | 3 | 4 | 
| Jeny | B | 5 | 5 | 6 | 
| Jeny | C | 7 | 6 | 3 | 
+-------+-------+-------+-----+-----+ 
| TOTAL + A | 17.6 |17.6 |17.6 | 
| TOTAL + B | 19 | 19 | 19 | 
| TOTAL + C | 14 | 14 | 14 | 
+-------+-------+-------+-----+-----+ 

我想有结果像波纹管:

+-------+-------+-------------------+ 
|  | Sales | Earned by date ($)| 
| Name | Code |-------+-----+-----+ 
|  |  | 1 | 2 | 3 | 
+-------+-------+-------+-----+-----+ 
| Jhon | A | 4.5 | 7 | 2 | 
| Jhon | B | 1.5 | 7 | 5 | 
| Jhon | C | 8.2 | 4 | 4 | 
| Ryan | A | 4 | 6 | 3 | 
| Ryan | B | 5 | 7 | 2 | 
| Ryan | C | 2.9 | 9 | 7 | 
| Jeny | A | 9.1 | 3 | 4 | 
| Jeny | B | 5 | 5 | 6 | 
| Jeny | C | 7 | 6 | 3 | 
+-------+-------+-------+-----+-----+ 
| TOTAL + A | 17.6 | 16 | 9 | 
| TOTAL + B | 11.5 | 19 | 13 | 
| TOTAL + C | 18.1 | 19 | 14 | 
+-------+-------+-------+-----+-----+ 

非常感谢您的帮助。

回答

0

另一种替代方法,而不是硬编码这些组合数字,只是创建这些总数的数组。首先创建总计,然后呈现它们。换句话说,先将它们分组。

<table border="1" width=""> 
    <tr> 
     <th rowspan="2">Name</th> 
     <th rowspan="2">Sales code</th> 
     <th colspan="3">Earned by date ($)</th> 
    </tr> 
    <tr> 
     <th>1</th> 
     <th>2</th> 
     <th>3</th> 
    </tr> 

    <?php 
    $grand_total = array(); 
    foreach($data as $row){ ?> 
    <tr> 
     <td><?=$row['name']?></td> 
     <td><?=$row['sales_code']?></td> 
     <td><?=$row['1']?></td> 
     <td><?=$row['2']?></td> 
     <td><?=$row['3']?></td> 
    </tr> 
    <?php 
     for($i = 1; $i <= 3; $i++) { 
      if(!isset($grand_total[$row['sales_code']][$i])) { 
       $grand_total[$row['sales_code']][$i] = 0; 
      } 
      $grand_total[$row['sales_code']][$i] += $row[$i]; 
     } 
    } 
    ?> 

    <?php foreach($grand_total as $code => $total_row) { ?> 
    <tr> 
     <td>TOTAL</td> 
     <td><?php echo $code; ?></td> 
     <?php foreach($total_row as $t) { ?> 
     <td><?php echo $t; ?></td> 
     <?php } ?> 
    </tr> 
    <?php } ?> 

</table> 
+0

谢谢@幽灵,你救了我的一天。 –

+0

@HarapanHarefa确实很高兴这有帮助 – Ghost

0

你只需要更多的计数器/变量。如果你更了解你的数据结构,你可以让这张表更灵活,更强大。但长手,脆弱的方式是...

<table border="1" width=""> 
     <tr> 
      <th rowspan="2">Name</th> 
      <th rowspan="2">Sales code</th> 
      <th colspan="3">Earned by date ($)</th> 
     </tr> 
     <tr> 
      <th>1</th> 
      <th>2</th> 
      <th>3</th> 
     </tr> 
     <?php 
     $a_tot1 = 0; 
     $a_tot2 = 0; 
     $a_tot3 = 0; 
     $c_tot1 = 0; 
     $c_tot2 = 0; 
     $c_tot3 = 0; 
     $b_tot1 = 0; 
     $b_tot2 = 0; 
     $b_tot3 = 0; 
     foreach($data as $row){ 
      ?> 
      <tr> 
       <td><?=$row['name']?></td> 
       <td><?=$row['sales_code']?></td> 
       <td><?=$row['1']?></td> 
       <td><?=$row['2']?></td> 
       <td><?=$row['3']?></td> 
      </tr> 
      <?php 
      $a_tot1 += ($row['sales_code'] == "A" ? $row['1'] : 0); 
      $a_tot2 += ($row['sales_code'] == "A" ? $row['2'] : 0); 
      $a_tot3 += ($row['sales_code'] == "A" ? $row['3'] : 0); 
      $b_tot1 += ($row['sales_code'] == "B" ? $row['1'] : 0); 
      $b_tot2 += ($row['sales_code'] == "B" ? $row['2'] : 0); 
      $b_tot3 += ($row['sales_code'] == "B" ? $row['3'] : 0); 
      $c_tot1 += ($row['sales_code'] == "C" ? $row['1'] : 0); 
      $c_tot2 += ($row['sales_code'] == "C" ? $row['2'] : 0); 
      $c_tot3 += ($row['sales_code'] == "C" ? $row['3'] : 0); 
     } 
     ?> 
     <tr> 
      <td>TOTAL</td><td>A</td> <td><?=$a_tot1?></td> <td><?=$a_tot2?></td> <td><?=$a_tot3?></td> 
     </tr> 
     <tr> 
      <td>TOTAL</td><td>B</td> <td><?=$b_tot1?></td> <td><?=$b_tot2?></td> <td><?=$b_tot3?></td> 
     </tr> 
     <tr> 
      <td>TOTAL</td><td>C</td> <td><?=$c_tot1?></td> <td><?=$c_tot2?></td> <td><?=$c_tot3?></td> 
     </tr> 

    </table> 
+0

谢谢@ D-walsh ... –

+0

如果这对你有用,请接受答案。谢谢! –

+0

感谢您的帮助,但其他人给出了最恰当的答案,因为我需要一个支持动态内容的代码 –