CakePHP:无法确定员工的总工作时间
我想要的:-我想根据几天的工作时间计算我的员工的总工作时间 我的问题总工作时间($total_hours)不工作 我的模型 班级出勤率模型{CakePHP:无法确定员工的总工作时间,php,mysql,cakephp,Php,Mysql,Cakephp,我想要的:-我想根据几天的工作时间计算我的员工的总工作时间 我的问题总工作时间($total_hours)不工作 我的模型 班级出勤率模型{ function add($data){ if (!empty($data)) { $this->create(); if($this->save($data)) { return true ; } } } function fetchdata() {
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'),
)));
}
}
我的看法
代码显示有一个数组。该数组包含(每个点的考勤id和进出时间)。
重要的是,您应该检查如何填充此数组。
在上面通过$employees数组生成表的foreach中,这里有(employeen_id)的名称(id)
所以你应该在你的视图中写一个新的查询!!!在你的第一个前段和第二个前段中间
在这一行之前:
$total_hours=0
您必须像这样编写查询并从数据库中获取数据:
//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.
//从Attendances中选择*
其中attendences.date>“2014-04-23”和attendences.date<“2014-04-30”
id=$e['Employee']['id']//是第一个数组中的员工id。
因此,当您获取数据时,您有一个名为“$Attentince”的新数组
然后,你的第二个foreach(计算工资和总工时)应该能正常工作plz,用表格中的少量数据更新你的问题……你的朋友给了你一个奇怪的解决方案。。。将数据库用于简单的任务mysql sum()。看,是的,它更容易。。。。它起作用了
<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>
}
//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.