我正在计算 total_brb_time ,但无法为每位员工计算。实际上我想将 total_brb_time 添加到考勤数组,并希望将其传递给名为'attendance-sheet'的视图。
这是我的控制者:
public function generate_report()
{
if ($_GET)
{
$fromDate = $this->input->get('fromDate',TRUE);
$toDate = $this->input->get('toDate',TRUE);
$employeeId = $this->input->get('employeeId',TRUE);
if ($employeeId == 'none')
{
$data['attendance'] = $this->Attendance_Model->filter_attendance_for_report($fromDate, $toDate);
}
else
{
$data['attendance'] = $this->Attendance_Model->filter_user_attendance_for_report($fromDate, $toDate, $employeeId);
}
foreach ($data as $att)
{
$attDate = $att[0]->date;
$attUserId = $att[0]->user_id;
$brb = $this->Brb_Model->filter_brb_for_report($attDate, $attUserId);
foreach ($brb as $b)
{
$brbTime = $b->brb_time;
$backTime = $b->back_time;
$brbDate = $b->date;
$start_time = new DateTime($brbDate.$brbTime);
$end_time = new DateTime($brbDate.$backTime);
$dateDiff = $start_time->diff($end_time);
$brb_time[] = $dateDiff->format("%H:%I:%S");
}
$sum = strtotime('00:00:00');
$sum2 = 0;
foreach ($brb_time as $v)
{
$sum1 = strtotime($v) - $sum;
$sum2 = $sum2 + $sum1;
}
$sum3 = $sum + $sum2;
$total_brb_time = date("H:i:s", $sum3);
$data['brbtime'] = $total_brb_time;
}
$data['employees'] = $this->User_Model->get_users();
$this->load->view('attendance-sheet', $data);
}
}
这是我的Attendance_Model
功能:
public function filter_attendance_results($query)
{
$sql = 'select attendance_id, date, time_in, time_out, notes, working_hours, a.user_id, a.first_name, a.last_name, a.team, a.designation from users AS a
LEFT JOIN attendance on a.user_id= attendance.user_id ';
$sql .= 'where 1=1 '.$query ;
$query = $this->db->query($sql)->result();
return $query;
}
public function filter_attendance_for_report($date1, $date2)
{
$query = $this->db->select('*')
->from('attendance')
->join('users', 'attendance.user_id = users.user_id')
->where('date >=', $date1)
->where('date <=', $date2)
->order_by("date", "asc")
->get();
return $query->result();
}
public function filter_user_attendance_for_report($date1, $date2, $user_id)
{
$query = $this->db->select('*')
->from('attendance')
->join('users', 'attendance.user_id = users.user_id')
->where('date >=', $date1)
->where('date <=', $date2)
->where('attendance.user_id =', $user_id)
->order_by("date", "asc")
->get();
return $query->result();
}
我想显示表格行中每位员工的总brb。但它只显示每行的第一个计算的brb。这是观点:
<div class="x_content">
<table id="datatable-buttons" class="table table-striped table-bordered">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Date</th>
<th>Working Hours</th>
<th>Total BRB Time</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
foreach ($attendance as $att):
?>
<tr>
<th scope="row"><?= $i++ ?></th>
<td><?= $att->first_name .' '. $att->last_name ?></td>
<td><?= $att->date ?></td>
<td><?= $att->working_hours ?></td>
<td><?= $brbtime ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
答案 0 :(得分:0)
$data['brbtime'] = $total_brb_time;
行{p>中,您将键brbtime
添加到所有数组$data
。如果要将brbtime
添加到数组$data
的每个元素,请执行下一步:
而foreach ($data as $att)
更改为foreach ($data as $key=>$att)
而$data['brbtime'] = $total_brb_time;
更改为$data[$key]['brbtime'] = $total_brb_time;