I am following a tutorial about CRUD operations in mysql database, concretely with PDO (I don't think it is relevant though).
It is a time tracker; formed by a projects table and a task table; every task belong to a project. A project can have multiple task but a task can only belong to one project. I need to display a "report" page, which will include the project, then the list of task for that project with the time spent on each task, and at the end of each project, the total time spent for that project.
This is the table to display the report; the last project does not have the total, but it is a matter further discussed on the tutorial.
<?php
$filter = 'all';
?>
<table>
<?php
$total = $project_id = $project_total = 0;
foreach (get_tasks_list($filter) as $item) {
if ($project_id != $item['project_id']) {
if ($project_id > 0) {
echo '<tr>';
echo '<th colspan="2">Project Total</th>';
echo '<th>' . $project_total . '</th>';
echo '</tr>';
$project_total = 0;
}
$project_id = $item['project_id'];
echo '<thead>';
echo '<tr>';
echo '<td>' . $item['project'] . '</td>';
echo '<td> Date </td>';
echo '<td> Time </td>';
echo '</tr></thead>';
}
$project_total += $item['time'];
$total += $item['time'];
echo '<tr>';
echo '<td>' . $item['title'] . '</td>';
echo '<td>' . $item['date'] . '</td>';
echo '<td>' . $item['time'] . '</td>';
echo '</tr>';
}
?>
<tr class="totalReportTime">
<th colspan="2">Total</th>
<th><?php echo $total; ?></th>
</tr>
</table>
get_tasks_list gives the list of all tasks.
This is how the reports table looks:
My doubt is..... What am I doing on: $project_id = $item['project_id']; ??
I mean.... after the foreach loop, foreach task I check if the $project_id I've just set to 0 is not equal to the project_id related with the task, but then, why am I saying if the $project_id is greater than 0? and why after it it is setting the value for $project_id as the same value of the project_id from the task?
Basically, I am not catching how it "knows" that it needs to add the total for each project at the end, or why it does that assignment $project_id = $item['project_id'];
Any basic explanation will help.
Thanks!