[英]mysql CRUD understanding the loop
我正在关注有关mysql数据库中CRUD操作的教程,具体来说是与PDO一起使用的(尽管我认为这并不重要)。
它是一个时间跟踪器; 由项目表和任务表组成; 每个任务都属于一个项目。 一个项目可以有多个任务,但是一个任务只能属于一个项目。 我需要显示一个“报告”页面,其中将包括项目,然后是该项目的任务列表以及在每个任务上花费的时间,在每个项目结束时,将花费在该项目上的总时间。
这是显示报告的表; 最后一个项目没有总计,但这是在教程上进一步讨论的问题。
<?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给出所有任务的列表。
我的疑问是.....我在做什么: $ project_id = $ item ['project_id']; ??
我的意思是.... foreach循环之后,foreach任务我检查我刚刚设置为0的$ project_id是否等于与该任务相关的project_id,但是,为什么我要说$ project_id是否更大大于0? 为什么在它之后将$ project_id的值设置为与任务中的project_id相同的值?
基本上,我没有了解它如何“知道”它需要在最后添加每个项目的总数,或者为什么它要执行该分配$ project_id = $ item ['project_id'];
任何基本解释都会有所帮助。
谢谢!
我已经为每个代码部分添加了注释,以便您阅读它们并了解每个代码的重要性,还有任何问题请告诉我。
<?php
$filter = 'all';
?>
<table>
<?php
// variable declaration
$total = $project_id = $project_total = 0;
// fetaching all the tasks
foreach (get_tasks_list($filter) as $item) {
// $item['project_id'] --> it has the project_id shows this task belongs to this project
// this condition checks that current project_id and task's project id is matched or not
// at first time $project_id will be zero so this will be true at first time
// and next time when it will be set to another project id again this will be executed.
// so in short this condition has 2 purpose
// 1. show project total
// 2. show project title
// it will done once project is changed.
if ($project_id != $item['project_id']) {
// at very first time we will not show total for first project
// so we are comparing it will 0
if ($project_id > 0) {
// print total
echo '<tr>';
echo '<th colspan="2">Project Total</th>';
echo '<th>' . $project_total . '</th>';
echo '</tr>';
// once total is printed we need to reset it for another project
$project_total = 0;
}
// this is needed for printing header only once for each tasks.
// as if ($project_id != $item['project_id'])
// this condition only pass when project is changed.
// so it will print total and then header of project
// then we will assign $project_id = $item['project_id'] as we need to tell loop
// current project id is changed or not from previous iteration if its changed then
// again print total and header with this condition check if ($project_id != $item['project_id'])
$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>';
}
// this will be total for each task and you can see
// it is cleared in upper condition when project is changed
$project_total += $item['time'];
// this is grand total it will add all the time for all the task and not going to reset
$total += $item['time'];
// this will print each task
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
// at last row we are printing main total
echo $total;
?></th>
</tr>
</table>
最后一个项目没有总数
这是因为您可以看到它的最后一个任务,所以循环不会再次自我迭代,也不会比较project_id的变化,因此它不会达到这种状态,因此不会打印最后一个项目
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.