简体   繁体   English

mysql CRUD理解循环

[英]mysql CRUD understanding the loop

I am following a tutorial about CRUD operations in mysql database, concretely with PDO (I don't think it is relevant though). 我正在关注有关mysql数据库中CRUD操作的教程,具体来说是与PDO一起使用的(尽管我认为这并不重要)。

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. get_tasks_list给出所有任务的列表。

This is how the reports table looks: 这是报告表的外观: 在此处输入图片说明

My doubt is..... What am I doing on: $project_id = $item['project_id']; 我的疑问是.....我在做什么: $ 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? 我的意思是.... foreach循环之后,foreach任务我检查我刚刚设置为0的$ project_id是否等于与该任务相关的project_id,但是,为什么我要说$ project_id是否更大大于0? and why after it it is setting the value for $project_id as the same value of the project_id from the task? 为什么在它之后将$ project_id的值设置为与任务中的project_id相同的值?

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']; 基本上,我没有了解它如何“知道”它需要在最后添加每个项目的总数,或者为什么它要执行该分配$ project_id = $ item ['project_id'];

Any basic explanation will help. 任何基本解释都会有所帮助。

Thanks! 谢谢!

I have added comments for each code part so you can read them and understand each code's importance, still have any question please let me know. 我已经为每个代码部分添加了注释,以便您阅读它们并了解每个代码的重要性,还有任何问题请告诉我。

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

the last project does not have the total 最后一个项目没有总数

Its because you can see its last task so loop will not iterate it self again and there will no comparison of project_id change so it will not go to that condition so it wont print last projects total 这是因为您可以看到它的最后一个任务,所以循环不会再次自我迭代,也不会比较project_id的变化,因此它不会达到这种状态,因此不会打印最后一个项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM