简体   繁体   中英

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).

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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