[英]PHP Conditional row calculation
我有一个数组,我已经显示在一个像波纹管的表中:
+-------+-------+-------------------+
| | 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 |
+-------+-------+-------+-----+-----+
非常感谢您的帮助。
你只需要更多的计数器/变量。 如果您对数据结构有了更多了解,可以使这个表格更灵活,更健壮。 但长期,脆弱的方式是......
<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>
另一种替代方案,而不是硬编码这些组合数字,只需创建这些总数的数组。 首先创建总计,然后显示它们。 换句话说,先将它们分组。
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.