dongtuliao6760 2016-10-26 13:37
浏览 13
已采纳

mysql CRUD理解循环

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: enter image description here

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!

  • 写回答

1条回答 默认 最新

  • doufei4418 2016-10-26 13:56
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真