我如何计算在foreach循环中的当前出勤率,其中attendnace_status == 1为现在

时间:2017-05-30 05:14:20

标签: php mysql codeigniter

如何计算attendnace_status == 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): ?>
            <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循环中的当前出勤总数

<th>Present</p>下我想要计算每个员工在foreach循环中的总出勤率。如果员工P状态25天,那么现在计数25

5 个答案:

答案 0 :(得分:0)

使用简单的计数器。

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

答案 1 :(得分:0)

<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;打印1

答案 2 :(得分:0)

您需要将每个员工重置为0,如此

<?php foreach ($attendance as $key => $v_employee): 

 $ctr=0;

?>
 .........

<?php endforeach; ?> 

答案 3 :(得分:0)

您必须为每位员工添加一个计数器变量,当存在当前状态时,该变量将增加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>

答案 4 :(得分:0)

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