CakePHP:无法确定员工的总工作时间

时间:2014-04-26 07:10:50

标签: php mysql cakephp

我想要的是什么: - 我想根据多天的工作时间来计算员工的总工作时间。

我的问题总工时($ 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>

我的程序员朋友给了我一个解决方案。但我不知道如何在CAKEPHP中实施

我也在更新解决方案

看这里:

  

$ 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(计算工资和总工时)应该正常工作

0 个答案:

没有答案