PHP条件行计算
我有一个数组,我已经显示在一个表中,如下面所示:PHP条件行计算,php,arrays,Php,Arrays,我有一个数组,我已经显示在一个表中,如下面所示: +-------+-------+-------------------+ | | Sales | Earned by date ($)| | Name | Code |-------+-----+-----+ | | | 1 | 2 | 3 |... end of month +-------+-------+-------+-----+-----+ | Jhon | A |
+-------+-------+-------------------+
| | 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 |
+-------+-------+-------+-----+-----+
+-------+-------+-------------------+
| | 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 |
+-------+-------+-------+-----+-----+
在表的最后一行中,我希望根据包含相同销售代码的行的摘要填写每个日期的总行值
我尝试了以下语法:
<?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 |... 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 |
+-------+-------+-------+-----+-----+
+-------+-------+-------------------+
| | 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>
名称
销售代码
按日期划分的收入(美元)
1.
2.
3.
托塔拉
TOTALB
TOTALC
您只需要更多计数器/变量。如果您对您的数据结构了解得更多,您可以使此表成为一种更灵活、更健壮的方式。但那长长的手,脆弱的路是
<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>
名称
销售代码
按日期划分的收入(美元)
1.
2.
3.
托塔拉
TOTALB
TOTALC
另一种选择是,不必硬编码这些组合数字,只需创建这些总数的数组即可。首先创建总数,然后显示它们。换句话说,先把他们分组
<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>
名称
销售代码
按日期划分的收入(美元)
1.
2.
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
$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>
名称
销售代码
按日期划分的收入(美元)
1.
2.
3.
全部的
谢谢@Ghost,你救了我一天。@HarapanHarefa当然很高兴这个帮助谢谢@Ghost,你救了我一天。@HarapanHarefa当然很高兴这个帮助如果对你有用,请接受答案。谢谢谢谢你的帮助,但是其他人给出了最合适的答案,因为我需要一个支持动态内容的代码,然后在问题中说明!“如果你对你的数据结构了解得更多,你可以让这个表变得更灵活、更健壮。”浪费每个人的时间。如果这对你有用,请接受答案。谢谢谢谢你的帮助,但是其他人给出了最合适的答案,因为我需要一个支持动态内容的代码,然后在问题中说明!“如果你对你的数据结构了解得更多,你可以使这个表变得更灵活、更健壮。”浪费每个人的时间。