我有一个工作日历功能,还有一个工作总和,可以从时间跟踪器计算项目工时,但是我正在努力将它们组合成一个工作功能。
查看代码中的注释以了解当前进度,下面还提供了一个打印屏幕。
如何获取查询以基于日历中每一行的$ year $ month和$ day_num回显SUM?
这是我当前的代码:
<?php
include 'db.php';
// GET USER DATA
$sql = "SELECT * FROM users where user_username = '".$_SESSION['username']."'";
$result = $mysqli->query($sql);
while($row = $result->fetch_assoc()) {
$loginuser_id = $_GET['id'];
}
// GET TIME TRACKING DATA
$query = "
SELECT userhours_id, user_hours, hours_timeoccur, SUM(user_hours) as 'myhours' FROM hours h
where h.userhours_id = $loginuser_id
AND h.hours_timeoccur = '2018-09-15'
";
$result = $mysqli->query( $query ) or die($mysqli->error);
$row = $result->fetch_assoc();
$user_hours = $row['myhours'];
// VIEW THE SUM OF HOURS BASED ON THE DAY FROM THE QUERY ABOVE
echo $user_hours;
// THE CALENDAR
showmonth($m = $_GET['m'] ,$y = $_GET['y']);
function showmonth($month, $year) {
$first_day = mktime(0,0,0,$month, 1, $year);
$title = date('F', $first_day);
$day_of_week = date('D', $first_day);
$days_in_month = cal_days_in_month(0, $month, $year);
echo "<div id='container'>";
echo "<div id='datenav'>";
echo "<div id='prevbtn'>prev</div>";
echo "<div id='date'>$title $year</div>";
echo "<div id='nextbtn'>next</div>";
echo "</div>";
echo "<div id='cal'>";
$day_num = 1;
while ( $day_num <= $days_in_month ) {
$cnt = 0;
while ($cnt < 7) {
echo "<div id='days'>";
echo "<h4>$day_num</h4>";
echo "<h5>40h</h5>"; // HERE I WANT THE SUM OF TIME TRACKING BY EACH DAY
echo "</div>";
$day_num++;
if ($day_num > $days_in_month) {break;}
}
}
echo "</div>";
echo "</div>";
} // end of function
?
当前输出:(146是QUERY中指定日期的所有小时的总和,并且图片中的所有40h都是手动编写的。这是我每天要回显QUERY输出的地方)
期望的输出将是从日历中列出的那一天开始的146,并且每隔一天都是相同的,但带有这些天的小时数。
在此先感谢您,如果需要更多数据,请告诉我!
样本数据:
DB: hours
hours_id userhours_id projecthours_id user_hours hours_timeoccur
1 5 12377 5 2018-08-14
2 3 12378 100 2018-09-15
3 3 12378 46 2018-09-15
4 1 12378 5 2018-09-16
5 5 12379 5 2018-09-17
DB: users
user_id user_username .....
1 Adam
2 Bryan
3 Clinton
4 David
5 Eric
DB: projects
project_id project_name .....
1 My first test
2 My second test
3 My third test
4 My fourth test
5 My fifth test
答案 0 :(得分:0)
想通了!
我在日历代码中移动了QUERY,然后一切都完美了!