使用if-else语句php代码点火器嵌套forloop


nested forloop with if else statement php codeigniter

查看图像

my controller
public function admin() {
        $data['employee'] = $this->attendance_model->get_attendance();
        // get today's all employee attendance details
        $data['attendance'] = $this->attendance_model->attendance();
        // attendance page
        $data['header'] = "Employee";
        $data['sub_header'] = "Attendance employee";
        $data['main_content'] = 'attendance/admin_list';
        $this->load->view('employeelayout/main',$data);
    }
my model
// get the employee details
    public  function get_attendance () {
        return $this->db->get('employee')->result();
    }
    
    public function attendance() {
        $today = date('Y-m-d');  // get today date
        $this->db->where('A.date',$today)->from('attendance A')
        ->join('employee E','E.employee_id = A.employee_id','LEFT');
        return $this->db->get()->result();
    }
<thead>
                <tr>
                    <th>#</th>             
                    <th>Employee Name</th>
                    <th>Employee Number</th>
                     <th>Today</th>
                    <th>Mark Incoming</th>       
                    <th class="hidden-phone">Status</th>
                </tr>
            </thead>
           
            <tbody>
                <?php $count = 1; ?>
                <?php foreach($employee as $emp) {  ?>
                 
               <tr class="gradeX">
                    <td><?php echo $count ++ ?></td>
                    <td><?php echo $emp->first_name.' '.$emp->last_name ?></td>
                    <td><?php echo $emp->employee_number ?></td>
                    <?php foreach($attendance as $atten) {  ?>
                    <?php if($emp->employee_id == $atten->employee_id) { ?> 
                    <td><?php echo $atten->date ?></td>
                    <td><?php echo $atten->mark_in_time ?></td>
                    <td><?php echo "present" ?></td>
                    <?php } else {  ?>
                    <td></td>
                    <td></td>
                    <td><?php echo "absent" ?></td>
                    <?php } ?>
                    <?php } ?>
               </tr> 
               
                <?php } ?>
            </tbody>

我使用了嵌套foreach,第一个我在第一个foreach中显示所有员工第二,我想显示员工出席情况,包括出席和缺席从考勤表中,我将employee_id与employee_id的员工表进行匹配在那里它显示所有在场的员工并且它正确地显示缺席的员工。请帮忙

我发布了图片,你可以看到,我显示了所有员工,我正在检查员工id是否为员工id(表示存在于考勤表中)。如果存在,则显示"存在",否则显示"不存在"。但它循环过度。

SELECT e.*, CASE WHEN (a.attendance_id is null) THEN 'absent' ELSE 'present' END as presentinfo FROM `employee` e left join attendance a on e.employee_id=a.employee_id and a.date=date(now())

您的Html页面看起来像:

           <tr class="gradeX">
                <td><?php echo $emp->first_name.' '.$emp->last_name ?></td>
                <td><?php echo $emp->employee_number ?></td>
                <td><?php echo $emp->presentinfo ?></td>
           </tr> 
            <?php } ?>
        </tbody>

我的建议:使用上面的查询。。它将显示您需要的结果。

尝试以下

您的型号

public  function get_attendance () {
    $arrEmployee = $this->db->get('employee')->result();
    $arrEmployeeById = array();
    $arrEmployeeIds = array();
    foreach($arrEmployee AS $objEmployee)
    {
        $objEmployee->arrAttendance = array();
        $arrEmployeeById[$objEmployee->employee_id] = $objEmployee;
        $arrEmployeeIds[] = $objEmployee->employee_id;
    }
    $this->db->where_in("A.employee_id",$arrEmployeeIds);
    $arrAttendances = $this->attendance();
    foreach($arrAttendances AS $objAttendence)
    {
        if (isset($arrEmployeeById[$objAttendence->employee_id]))   $arrEmployeeById[$objAttendence->employee_id]->arrAttendance[] = $objAttendence;
    }
    return $arrEmployee;
    //return $this->db->get('employee')->result();
}
public function attendance() 
{
    $today = date('Y-m-d');  // get today date
    $this->db->where('A.date',$today)->from('attendance A')
    //->join('employee E','E.employee_id = A.employee_id','LEFT');
    return $this->db->get()->result();
}

您的控制器

public function admin() {
    $data['employee'] = $this->attendance_model->get_attendance();
    $data['header'] = "Employee";
    $data['sub_header'] = "Attendance employee";
    $data['main_content'] = 'attendance/admin_list';
    $this->load->view('employeelayout/main',$data);
}

//您的视图

<thead>
    <tr>
        <th>#</th>             
        <th>Employee Name</th>
        <th>Employee Number</th>
         <th>Today</th>
        <th>Mark Incoming</th>       
        <th class="hidden-phone">Status</th>
    </tr>
</thead>
<tbody>
    <?php $count = 1; ?>
    <?php foreach($employee as $emp) {  ?>
   <tr class="gradeX">
        <td><?php echo $count ++ ?></td>
        <td><?php echo $emp->first_name.' '.$emp->last_name ?></td>
        <td><?php echo $emp->employee_number ?></td>
        <?php 
        if (count($emp->arrAttendance) > 0)
        {
            foreach($emp->arrAttendance as $atten) {  
        ?>
                <td><?php echo $atten->date ?></td>
                <td><?php echo $atten->mark_in_time ?></td>
                <td><?php echo "present" ?></td>
        <?php
            }
        }
        else 
        {  
        ?>
            <td></td>
            <td></td>
            <td><?php echo "absent" ?></td>
        <?php } ?>
   </tr> 
    <?php } ?>
</tbody>