从时间跟踪器到日历的总时数

I've got a working calendar function, and a working sum to calculate the project hours from the time tracker, but I'm struggeling to combine them into a working function.

See comments in code to figure out the current progress, a printscreen is also provided below.

How can I get the QUERY to echo out the SUM based on the $year $month and $day_num on each row in the calendar?

This is my current code:

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

?

The current output: (146 is the SUM of all the hours on the specified date in the QUERY, and all the 40h in the picture are manually written. That's the place that I want to echo the QUERY output for each day) enter image description here

The desired output would be to have 146 on the day it's summarized from listed in the calendar, and the same for each and every other day but with those days hours.

Big thanks in advance and let me know if you need any more data!

Sample data:

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

Figured it out!

I moved the QUERY inside the calendar code and then it all worked out just perfect!