gpt4 book ai didi

php - CAKEPHP:使用两个模型使用一个 View 和一个 Controller 在一张表中显示数据错误: undefined variable :total_hours

转载 作者:行者123 更新时间:2023-11-30 00:20:22 26 4
gpt4 key购买 nike

我想做的是出勤模型中获取fetchdata函数,并为获取函数>员工模型

我的问题:-

问题1注意(8): undefined variable :total_hours [APP\View\Employees\index.ctp,第 32 行]问题2值未插入到同一个表中

EmployeesController.php

<?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());

}



}

Attendence.php(模型)

class Attendence extends 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')
)));


}

}

Employee.php(模型)

class Employee extends 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') )
);
} }

index.ctp(与EmployeesController附加的VIEW)

<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>

最佳答案

您正在使用/增加尚未启动的变量。

更新这一行:

<?php foreach ($attendence as $et){

至:

<?php $total_hours = 0; foreach ($attendence as $et){

关于php - CAKEPHP:使用两个模型使用一个 View 和一个 Controller 在一张表中显示数据错误: undefined variable :total_hours,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23294874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com