【问题标题】:Displaying the nested foreach with if else in php codeigniter在 php codeigniter 中使用 if else 显示嵌套的 foreach
【发布时间】:2016-08-16 21:41:03
【问题描述】:

控制器

public function admin()
{
    // get the current date employee logged in details
    $data['get_attendance'] = $this->attendance_model->get_attendance();

    // get the active employee details
    $data['get_employee'] = $this->attendance_model->get_employee();

    //print_r($data['get_employee']); exit();

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

型号

public function get_attendance()
{
    return $this->db->where('date',date('Y-m-d'))->get('attendance')->result_array();
}

public function get_employee()
{
    return $this->db->where('is_active',1)->get('employee')->result_array();
}

观看次数

<?php $count = 1 ?>
<?php foreach ($get_employee as $employee) { ?>
<tr class="gradeX">
    <td><?php echo $count++ ?></td>
    <td><?php echo $employee['first_name'] . ' ' . $employee['last_name'] ?></td>
    <td><?php echo $employee['employee_number'] ?></td>
       <?php foreach ($get_attendance as $attendance) : ?>
        <!-- if employee exists -->
           <?php if($employee['employee_id'] == $attendance['employee_id'] ) { ?>
            <td><?php echo date('M-Dj-Y',strtotime($attendance['date'])) ?></td>
            <td><?php echo $attendance['mark_in_time'] ?></td>
               <td>mark out going</td>
               <td>Active</td> 
            <?php  break; ?>

       <?php } elseif($employee['employee_id'] != $attendance['employee_id'] ) { ?>
           <td><?php echo "i'm absent" ?></td>
           <?php  break; ?>
      <?php  } ?>
       <?php endforeach; ?> 
    </tr> 

    <?php } ?>

我想显示当前登录(在场)的员工,同时也显示缺席的员工。我正在从员工表中获取员工详细信息。 和考勤表中的考勤详细信息。如果员工缺席并且我想显示缺席,则显示登录详细信息。这里的 foreach 循环在 if 条件下无法正确显示。循环有什么问题。

【问题讨论】:

    标签: php codeigniter if-statement foreach nested


    【解决方案1】:

    试试这个:

    <?php foreach ($get_employee as $employee) : ?>
        <tr class="gradeX">
            <td><?php echo $count++ ?></td>
            <td><?php echo $employee['first_name'] . ' ' . $employee['last_name'] ?></td>
            <td><?php echo $employee['employee_number'] ?></td>
               <?php foreach ($get_attendance as $attendance) : ?>
                <!-- if employee exists -->
                 <?php if($employee['employee_id'] == $attendance['employee_id']) : ?>
                    <td><?php echo date('M-Dj-Y',strtotime($attendance['date'])) ?></td>
                    <td><?php echo $attendance['mark_in_time'] ?></td>
                       <td>mark out going</td>
                       <td>Active</td>
    
                 <?php elseif ($employee['employee_id'] != $attendance['employee_id']) : ?>
                   <td><?php echo "i'm absent" ?></td>
    
                 <?php else : ?>
    
                 <?php endif; ?>
    
              <?php endforeach; ?> 
        </tr> 
    <?php endforeach; ?>
    

    【讨论】:

      【解决方案2】:

      应用左联接代替两个不同的查询。

      因为目前你已经应用了两个循环和它们的比较,会消耗大量的执行时间而且我不认为这是好的代码如果你得到成千上万的数据,你肯定可以分析执行时间。

      所以最好应用连接查询。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-08
        • 1970-01-01
        相关资源
        最近更新 更多