【发布时间】:2022-01-01 20:23:33
【问题描述】:
我正在使用 PHP 编写系统报告。我有一个包含两列(付款日期和金额)的表格。
如何使用 while 循环来计算每年 12 月之后的总金额?我尝试按照随附的屏幕截图执行此操作,但在每年 12 月之后显示的总和是错误的。
以下是我的做法:
<?php
$total=0;
$s = date('m');
$c = mysqli_query($con,"SELECT * FROM payroll_contribution_pssf WHERE employee_id='3' ORDER BY contribution_id ASC");
while($array = mysqli_fetch_array($c))
{
$m = date('m', strtotime($array['payroll_date']));
echo $array['payroll_date']." ".$array['employee_amount']."<br>";
if($m==12)
{
if($total == 0)
$total = $array['employee_amount'];
$total = $total + $array['employee_amount'];
echo number_format($total,0)."<br>";
}
}
?>
【问题讨论】:
-
请不要只显示屏幕截图然后谈论您应该尝试的内容,而是向我们展示您实际做了什么 - 代码! How to Ask, minimal reproducible example
-
$total=0; $s = 日期('m'); $c = mysqli_query($con,"SELECT * FROM payroll_contribution_pssf WHERE employee_id='3' ORDER BY contribution_id ASC"); while($array = mysqli_fetch_array($c)) { $m = date('m', strtotime($array['payroll_date'])); echo $array['payroll_date']." ".$array['employee_amount']."
"; if($m==12) { if($total == 0) $total = $array['employee_amount']; $total = $total + $array['employee_amount']; echo number_format($total,0)."
"; -
请不要在 cmets 中显示扩展的 sn-ps 代码,这很难阅读。编辑我们的问题并把它放在那里,格式正确stackoverflow.com/help/formatting
-
我已经更新了问题。很抱歉给您带来不便
-
您总结这些值的逻辑毫无意义。对于初学者,您通过将整个“计算”包装到
if($m==12) { ... }中忽略所有上个月的值