结果将根据codeigniter中的索引获得

时间:2017-02-22 10:34:12

标签: php arrays codeigniter

这是我的代码,用于显示获得的结果计数

我的控制器看起来像这样

$data['present'][]= $this->attendance_model->present_report_by_empid($v_employee->user_id,$date);

我的模型看起来像这样

public function present_report_by_empid($user_id = null,$date = null) 
 {


   $temp = explode("-",$date);
   $query='tbl_attendance.date_in';
   $this->db->where('tbl_attendance.attendance_status', 1);
   $this->db->where('tbl_attendance.user_id', $user_id);
   $this->db->where("YEAR(tbl_attendance.date_in)",$temp[0]);
   $this->db->where("MONTH(tbl_attendance.date_in)",$temp[1]);
   $result = $this->db->get('tbl_attendance')->result_array();
   $count=count($result); //counts number of rows
   return $count;
}

视图看起来像这样

 <?php foreach($present as $key1 => $row){?>
            <?php echo $row;?>
  <?php }?>

当我使用我的代码时,得到这样的结果 enter image description here

但我希望我的结果是这样的 enter image description here

2 个答案:

答案 0 :(得分:1)

看起来你正在构建$data['present']数组,首先包含每个人的数据,所以你可能想这样做: -

$data['present'][$v_employee->user_id] = $this->attendance_model->present_report_by_empid($v_employee->user_id,$date);

在视图中,您将传递$user_id,然后只回显正确用户的数据: -

<?php echo $present[$user_id] ;?>

答案 1 :(得分:1)

试试这个:

$data['present'][$v_employee->user_id]= $this->attendance_model->present_report_by_empid($v_employee->user_id,$date);

$empl = array(
//  empl_id => name,
    1 => 'administrator',
    2 => 'siraj',
    3 => 'faizal',
    4 => 'nesru',

)


foreach($empl as $key1 => $row){
    echo $row;
    if (!isset($data['present'][$key1])) {
        echo ' '.$data['present'][$key1];
    } else {
        echo ' - ';
    }
}