gpt4 book ai didi

php - 在PHP和MySql中创建一个累积比较表

转载 作者:行者123 更新时间:2023-11-30 22:24:34 26 4
gpt4 key购买 nike

我有一个名为 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

******注意***** 我认为我的解决方案不是解决我的问题的最佳方案。如果可能,请查看我想要的图片并找到最佳解决方案。我希望在实现目标或解决方案的任何步骤中得到帮助

我知道我正在使用 depricated mysql synthax,请忽略它并帮助解决我的问题。

<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>

最佳答案

这是对您提供的代码的重写,它使用虚拟随机数据而不是 DB,并且没有 POST 变量的逻辑,但您可以用您的代码替换。

<?php   
// session start, db connection goes here
echo '<table border="1" align="center">';
// Example of post vars
$start = new DateTime('2015-11-10');
$end = new DateTime('2016-02-28');
// Note: not sure why OP used modify here
$interval = new DateInterval('P1M');
$daterange = new DatePeriod($start, $interval, $end);

// Table Header row 1
echo '<tr><th>Day</th>';
foreach ($daterange as $date) {
echo '<th colspan="2">'.$date->format("F Y").'</th>';
}
echo '</tr>';

// Temporary month store
$months = array();

// Table Header row 2
echo '<tr style="background-color:#22bb22;"><th></th>';
foreach ($daterange as $date) {
$months[] = $date->format("F");
echo '<th>Daily</th>';
echo '<th>Cumulative</th>';
}
echo '</tr>';

// Table Body
$sumc = array();
for ($d = 1; $d <= 31; $d++) {
echo '<tr><td>'.$d.'</td>';
foreach ($months as $month) {
$db_date = $month.'-'.$d; // used for db query
// dummmy data (replace with db query result)
$sum = mt_rand(0, 999);
echo '<td>'.$sum.'</td>';
if(!array_key_exists($month, $sumc)) {
$sumc[$month] = 0;
}
$sumc[$month] = (int)$sum + $sumc[$month];
echo '<td>'.$sumc[$month].'</td>';
}
echo '</tr>';
}

echo '</table>';
?>

还有条件:

Where there is a weekend or the date is invalid i want the value to be zero.

因为数据库查询将返回 0 而假设这些都已处理是否正确?或者,即使数据库查询从 total_disb 返回 >0 的金额,您是否也必须检查周末的代码?

关于php - 在PHP和MySql中创建一个累积比较表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35646833/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com