我想要的是什么: - 我想根据多天的工作时间来计算员工的总工作时间。
我的问题总工时($ total_hours)无效。
类Attendence扩展了AppModel {
function add($data){ if (!empty($data)) { $this->create(); if($this->save($data)) { return true ; } } } function fetchdata() { return $this->find('all', array('conditions' => array('Attendence.date' > '2014-04-01', 'AND' => array('Attendences.date' < '2014-04-21'), ))); }
}
类EmployeesController扩展了AppController {
public $uses = array('Employee', 'Attendence', 'InsertDate'); public function add() { if($this->Employee->add($this->request->data)==true){ $this->redirect(array('action'=>'index')); } } public function index(){ $this->set('employees',$this->Employee->Fetch()); $this->set('attendence',$this->Attendence->fetchdata()); $this->set('dates',$this->InsertDate->fetchdate()); }
}
<div class="index">
<table>
<thead>
<th>Num</th>
<th>Employee</th>
<th>Salary/Hour</th>
<th>Start Date</th>
<th>End Date</th>
<th>Total Hour</th>
<th>Total Salary</th>
</thead>
<?php
$id = 0;
foreach($employees as $e):?>
<? $id++ ?>
<tr>
<td><?php echo $e{'Employee'}{'id'} ?></td>
<td><?php echo $e['Employee']['firstname'], $e['Employee']['lastname'] ?></td>
<td style="text-align:center"><?php echo $e['Employee']['salary'] ?></td>'
<?php foreach($dates as $d):?>
<td><?php echo $d['InsertDate']['start_date'] ?></td>
<td><?php echo $d['InsertDate']['end_date'] ?></td>
<?php
$total_hours = 0;
foreach ($attendence as $et){
$ts1 = strtotime($et['Attendence']['in_time']);
$ts2 = strtotime($et['Attendence']['out_time']);
$diff = abs($ts1 - $ts2) / 3600;
$total_hours += number_format($diff,2);
}
//Total hours
echo '<td style="text-align:center">'.$total_hours.'</td>';
//Total Salary
echo '<td style="text-align:center">'.$total_hours*$e['Employee']['salary'].'</td>';
?>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
</table>
</div>
我也在更新解决方案
看这里:
$ total_hours = 0; foreach($ attendence as $ et){ $ ts1 = strtotime($ et ['Attendence'] ['in_time']); $ ts2 = strtotime($ et ['Attendence'] ['out_time']); $ diff = abs($ ts1 - $ ts2)/ 3600; $ total_hours + = number_format($ diff,2);
}
代码显示有一个数组。数组包含(每个点的出勤ID和in_time和out_time)。 重要的是你应该检查如何填充这个数组。 在您通过$ employees数组生成表的上述foreach中,您在此处拥有(employee_id)名称(id) 所以你应该在你的视图中写一个新的查询!!!在你的第一个foreach中间和第二个foreach之前 在这一行之前:
$ total_hours = 0
您必须编写查询并从DB获取数据,如下所示:
//SELECT * FROM attendences
WHERE attendences.date > '2014-04-23' AND attendences.date < '2014-04-30'
AND id=$e['Employee']['id'] // is your employee_id in your first array.
因此,当您获取数据时,您有一个名为“$ attendence”的新数组 然后,你的第二个foreach(计算工资和总工时)应该正常工作