【问题标题】:how do i count total Present attendance in foreach loop where attendnace_status == 1 is as Present我如何计算foreach循环中的出席人数总数,其中attendnace_status == 1是出席
【发布时间】:2017-05-30 05:14:20
【问题描述】:

我如何计算 attendnace_status == 1 为 Present 的每个循环的总出席人数

<table id="" class="table table-bordered std_table">
    <thead>
        <tr>
            <th style="width:100%" class="col-sm-3">Name</th>                
            <?php foreach ($dateSl as $edate) : ?>                                
                <th class="std_p"><?php echo $edate ?></th>
            <?php endforeach; ?> 
            <th width="100%" style="width:100%" class="col-sm-3">Present</th>                    
        </tr>  
    </thead>  
    <tbody>
        <?php foreach ($attendance as $key => $v_employee): ?>
            <tr>  
                <td style="width: 100%" class="col-sm-3"><?php echo $employee[$key]->first_name . ' ' . $employee[$key]->last_name . ' ' . $employee[$key]->employment_id ?></td>   
                <?php foreach ($v_employee as $v_result): ?>
                    <?php foreach ($v_result as $emp_attendance): ?>
                        <td>
                            <?php
                            if ($emp_attendance->attendance_status == 1) {
                                echo '<span  style="padding:2px; 4px" class="label label-success std_p">P</span>';
                            }if ($emp_attendance->attendance_status == 2) {
                                echo '<span  style="padding:2px; 4px" class="label label-success std_p">HD</span>';
                            }if ($emp_attendance->attendance_status == '3') {
                                echo '<span style="padding:2px; 4px" class="label label-danger std_p">L</span>';
                            }if ($emp_attendance->attendance_status == '0') {
                                echo '<span style="padding:2px; 4px" class="label label-danger std_p">A</span>';
                            }if ($emp_attendance->attendance_status == 'H') {
                                echo '<span style="padding:2px; 4px" class="label label-info std_p">H</span>';
                            }
                            ?>
                        </td>
                        <td>
                        <?php endforeach; ?>   
                    <?php endforeach; ?> 
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

我如何计算 attendnace_status == 1 为出席的 foreach 循环中出席的总出席人数

&lt;th&gt;Present&lt;/p&gt; 下,我想计算每个员工在 foreach 循环中的总 P 出勤率。如果员工 P 状态 25 天,则当前计数 25

【问题讨论】:

  • 为attendnace_status==1声明一个value=0的变量,当attendnace_status == 1时增加1

标签: php mysql codeigniter


【解决方案1】:

您必须为每个员工添加一个计数器变量,当存在当前状态时,该变量将递增 1,

<table id="" class="table table-bordered std_table">
    <thead>
        <tr>
            <th style="width:100%" class="col-sm-3">Name</th>                
            <?php foreach ($dateSl as $edate) : ?>                                
                <th class="std_p"><?php echo $edate ?></th>
            <?php endforeach; ?> 
            <th width="100%" style="width:100%" class="col-sm-3">Present</th>                       
        </tr>  
    </thead>
    <tbody>
        <?php foreach ($attendance as $key => $v_employee): 
             $presentCounter = 0; // initialise counter variable here for every employee
        ?>
        <tr>  
            <td style="width: 100%" class="col-sm-3"><?php echo $employee[$key]->first_name . ' ' . $employee[$key]->last_name . ' ' . $employee[$key]->employment_id ?></td>   
            <?php foreach ($v_employee as $v_result): ?>

                <?php foreach ($v_result as $emp_attendance): ?>
                <td>
                    <?php
                    if ($emp_attendance->attendance_status == 1) {
                        $presentCounter++; // increment by 1
                        echo '<span  style="padding:2px; 4px" class="label label-success std_p">P</span>';
                    }if ($emp_attendance->attendance_status == 2) {
                        echo '<span  style="padding:2px; 4px" class="label label-success std_p">H</span>';
                    }if ($emp_attendance->attendance_status == '3') {
                        echo '<span style="padding:2px; 4px" class="label label-danger std_p">L</span>';
                    }if ($emp_attendance->attendance_status == '0') {
                        echo '<span style="padding:2px; 4px" class="label label-danger std_p">A</span>';
                    }if ($emp_attendance->attendance_status == 'H') {
                        echo '<span style="padding:2px; 4px" class="label label-info std_p">H</span>';
                    }
                    ?>
                </td>
                <?php endforeach; ?>   
            <?php endforeach; ?> 
            <td>
                <?php if(!isset($emp_attendance->attendance_status) || empty($emp_attendance->attendance_status) || !is_numeric($emp_attendance->attendance_status))
                {
                    $emp_attendance->attendance_status = 0;
                }
                echo $presentCounter; // print it
            ?>
            </td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>

【讨论】:

    【解决方案2】:

    使用简单的计数器。

    <?php $count = 0; ?>
    <?php foreach ($attendance as $key => $v_employee): ?>
        <tr>  
    
            <td style="width: 100%" class="col-sm-3"><?php echo $employee[$key]->first_name . ' ' . $employee[$key]->last_name . ' ' . $employee[$key]->employment_id ?></td>   
            <?php foreach ($v_employee as $v_result): ?>
                <?php foreach ($v_result as $emp_attendance): ?>
                    <td>
                        <?php
                        if ($emp_attendance->attendance_status == 1) {
                            $count++;
                            echo '<span  style="padding:2px; 4px" class="label label-success std_p">P</span>';
                        }if ($emp_attendance->attendance_status == 2) {
                            echo '<span  style="padding:2px; 4px" class="label label-success std_p">HD</span>';
                        }if ($emp_attendance->attendance_status == '3') {
                            echo '<span style="padding:2px; 4px" class="label label-danger std_p">L</span>';
                        }if ($emp_attendance->attendance_status == '0') {
                            echo '<span style="padding:2px; 4px" class="label label-danger std_p">A</span>';
                        }if ($emp_attendance->attendance_status == 'H') {
                            echo '<span style="padding:2px; 4px" class="label label-info std_p">H</span>';
                        }
                        ?>
                    </td>
    
                    <td>
    
                <?php endforeach; ?>       
    
            <?php endforeach; ?>     
    
        </tr>
    <?php endforeach; ?>
    
    <?php 'Total attendance: '.$count;?>
    

    【讨论】:

      【解决方案3】:
      <table id="" class="table table-bordered std_table">
                                  <thead>
                                      <tr>
                                          <th style="width:100%" class="col-sm-3">Name</th>                
      
                                          <?php foreach ($dateSl as $edate) : ?>                                
                                              <th class="std_p"><?php echo $edate ?></th>
                                          <?php endforeach; ?> 
                                              <th width="100%" style="width:100%" class="col-sm-3">Present</th>                       
      
                                      </tr>  
      
                                  </thead>      
      
                                  <tbody>
      
                                      <?php foreach ($attendance as $key => $v_employee): 
                                      $ctr = $ctr + $emp_attendance->attendance_status; ?>
                                          <tr>  
      
                                              <td style="width: 100%" class="col-sm-3"><?php echo $employee[$key]->first_name . ' ' . $employee[$key]->last_name . ' ' . $employee[$key]->employment_id ?></td>   
                                              <?php foreach ($v_employee as $v_result): ?>
                                                  <?php foreach ($v_result as $emp_attendance): ?>
                                                      <td>
                                                          <?php
                                                          if ($emp_attendance->attendance_status == 1) {
                                                              echo '<span  style="padding:2px; 4px" class="label label-success std_p">P</span>';
                                                          }if ($emp_attendance->attendance_status == 2) {
                                                              echo '<span  style="padding:2px; 4px" class="label label-success std_p">H</span>';
                                                          }if ($emp_attendance->attendance_status == '3') {
                                                              echo '<span style="padding:2px; 4px" class="label label-danger std_p">L</span>';
                                                          }if ($emp_attendance->attendance_status == '0') {
                                                              echo '<span style="padding:2px; 4px" class="label label-danger std_p">A</span>';
                                                          }if ($emp_attendance->attendance_status == 'H') {
                                                              echo '<span style="padding:2px; 4px" class="label label-info std_p">H</span>';
                                                          }
                                                          ?>
                                                      </td>
      
      
      
                                                  <?php endforeach; ?>   
      
      
                                              <?php endforeach; ?> 
      
                                              <td><?php
      
      
                                              $ctr=0;
                                               if ($emp_attendance->attendance_status == 1) {
                                               $ctr++;
                                               echo $ctr;
      
                                               }
      
                                               ?>
      
                                               </td>
      
      
      
      
      
      
      
                                          </tr>
                                      <?php endforeach; ?>
      
      
                                                                  </tbody>
                              </table>
      

      试过了,结果是1,意思是$echo ctr; print 1

      【讨论】:

        【解决方案4】:

        您需要像这样将计数器重置为 0 的每个员工

        <?php foreach ($attendance as $key => $v_employee): 
        
         $ctr=0;
        
        ?>
         .........
        
        <?php endforeach; ?> 
        

        【讨论】:

          【解决方案5】:
          <?php foreach ($attendance as $key => $v_employee): ?>
               <tr>
                   <?php $ctr = 0 ?>
          
                   <?php foreach ($v_employee as $v_result): ?>
          
                       <?php foreach ($v_result as $emp_attendance): ?>
                            <?php
                            if ($emp_attendance->attendance_status == 1) {
                                // = $ctr + $emp_attendance->attendance_status;
                                $ctr ++;
                                echo '<span  style="padding:2px; 4px" class="label label-success std_p">P</span>';
                            }
                  ?>
          
                      <?php endforeach; ?>
                  <?php endforeach; ?>
                  <td>
                      <?php echo $ctr;  ?>
                  </td>
                  <?php //reset the counter foreach employee ?>
                  <?php $ctr = 0 ?>
              </tr>
          <?php endforeach; ?>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-06-10
            • 1970-01-01
            • 2017-10-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-10-15
            • 2013-01-04
            相关资源
            最近更新 更多