[英]CAKEPHP: Using two Models to Show data in one table using one View and one Controller ERROR: Undefined variable: total_hours
What I am trying to do is Getting fetchdata function from Attendence Model and fetch function for Employee Model 我所试图做的是从考勤模式获取fetchdata功能,并获取函数员工型号
Problem 1 Notice (8): Undefined variable: total_hours [APP\\View\\Employees\\index.ctp, line 32] Problem 2 The Values are not getting inserted in the same table 问题1 通知(8):未定义变量:total_hours [APP \\ View \\ Employees \\ index.ctp,第32行]问题2 值未插入同一表中
<?php class EmployeesController extends AppController { public $uses = array('Employee', 'Attendence'); 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()); } }
class Attendence extends AppModel { 出席人数类扩展了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-23', 'AND' => array('Attendences.date' < '2014-04-30') ))); }
} }
class Employee extends AppModel { Employee类扩展AppModel {
function add($data){ if (!empty($data)) { $this->create(); if($this->save($data)) { return true ; } } } function Fetch() { return $this->find('all', array( 'fields' => array('Employee.id', 'Employee.firstname','Employee.lastname','Employee.salary') ) ); } }
<div class="index">
<table>
<thead>
<th>Num</th>
<th>Employee</th>
<th>Start Date</th>
<th>End Date</th>
<th>Salary/Hour</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>2014-04-24</td>
<td>2014-04-29</td>
<td style="text-align:center"><?php echo $e['Employee']['salary'] ?></td>'
</tr>
<?php endforeach; ?>
<?php 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>';
?>
</table>
</div>
<div class="actions">
<h3>Actions</h3>
<ul>
<li><a href="/users/add">New Employee</a></li>
<li><a href="/user_types">Attendance</a></li>
<li><a href="/users/add">Salary Calculator</a></li>
</ul>
</div>
You are using/incrementing a variable that isn't initiated yet. 您正在使用/增加尚未启动的变量。
update this line: 更新此行:
<?php foreach ($attendence as $et){
to: 至:
<?php $total_hours = 0; foreach ($attendence as $et){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.