簡體   English   中英

mysql CRUD理解循環

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM