【发布时间】:2016-02-27 09:50:09
【问题描述】:
我有一个名为 Payments 的数据库表,其中包含日期、金额字段。我想从金额字段中获取值并按日期对所有金额求和,然后将结果保存到 html 表中,然后像在图像上一样输出它们。
我动态地创建了日期,以便它们在哪个月份都相同,例如 1 月的 1-31 月和 2 月的 1-31 月。如果有周末或日期无效,我希望该值为零。我想要的是这样的表[table][1][1]:http://i.stack.imgur.com/iMOl3.jpg
这就是我得到的 [输出][1][1]:http://i.stack.imgur.com/MJpyT.jpg
******注意***** 我认为我的解决方案不是解决我的问题的最佳解决方案。如果可能的话,请查看我想要的图片并为我找到最佳解决方案。我希望在实现它的步骤或解决方案方面得到帮助
我知道我正在使用一个废弃的 mysql 合成器,请忽略它并帮助解决我的问题。
<table border="1" align="center">
<?php
session_start();
include("connection/db_con.php");
$sym='-';
$d=array();
///Insert values of month for period selected into an array
$a = $_POST['dat'];
$b = $_POST['dat2'];
$mnth=array();
$m_nam=array();
$m_nm=array();
$m_nam[]="Day";
//////New way of getting months in format Y-m
$start = new DateTime($a);
$start->modify('first day of this month');
$end = new DateTime($b);
$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
$mnth[]=$dt->format("Y-m");
$m_nam[]=date('F-Y', strtotime($dt->format("Y-m")));
$m_nm[]=date('M', strtotime($dt->format("Y-m")));
}
///////End of New way
echo "<tr bgcolor='#999999'>";
foreach ($m_nam as $m)
{
echo"<td>".$m."</td>";
}
echo"</tr>";
/////////End insert////////////////////////
$day=0;
for($x=1; $x<=31; $x++)
{
$day=$day+1;
echo"<tr>";
echo"<td>".$day."</td>";
$d=$sym.$x;
foreach($mnth as $mon)
{
$dat=$mon.$d;
$qry=mysql_query("SELECT SUM(amount) AS total_disb FROM payments where dat='$dat'")or die(mysql_error());
$row=mysql_fetch_assoc($qry);
$sum = $row['total_disb']+0;
echo"<td>".$sum."</td>";
}
echo"</tr>";
}
?>
</table>
【问题讨论】:
-
从您提供的输出屏幕截图中,假设您已成功输出每日金额并且缺少累积金额是否正确?
-
是的,非常正确。
标签: php html mysql arrays loops