我正在制定一个程序,根据每位员工的出勤情况计算餐费。这是我现在掌握的数据,一个名为考勤的表。这个表是从我导入的csv文件中填写的,这是我导入csv 的代码
function uploadData()
{
$fp = fopen($_FILES['userfile']['tmp_name'],'r') or die("can't open file");
// Ignore header line
$header = fgetcsv($fp);
// Array to store the partial records
$attendance= [];
while($csv_line = fgetcsv($fp))
{
// Key to identify first part of record (Name and date)
$key = $csv_line[1]."/".$csv_line[3];
if ( isset($attendance[$key]) ){
$start = $attendance[$key];
// Extract data from first part of record and add in end date from current row
$data = array(
'id_attend' => $start[0] ,
'emp_code' => $start[1],
'emp_name' => $start[2],
'date' => $start[3],
'time_in' => $start[4],
'time_out' => $csv_line[4],
);
$data['crane_features']=$this->db->insert('attendance', $data);
// Remove partial record
unset($attendance[$key]);
}
else {
// Store partial record
$attendance[$key] = $csv_line;
}
}
fclose($fp) or die("can't close file");
$data['success']="success";
}
这是我的桌子
| id_attend | emp_code | emp_name | date | time_in | time_out |
----------------------------------------------------------------------------------
| 0001 | brw | brown |01.01.2001| 07.00 | 20.00 |
| 0002 | cny | cony |01.01.2001| 07.00 | 20.00 |
----------------------------------------------------------------------------------
我需要计算他们参加的天数,并将其保存在我的数据库中。我已经有了一个名为折让的容器表
------------------------------------------------------------------------
|id_allowance(auto increment)|emp_code|emp_name|days_attended|allowance|
------------------------------------------------------------------------
| 0001 | brw | brown | 1 | 30.00 |
| 0002 | cny | cony | 1 | 30.00 |
------------------------------------------------------------------------
每次我导入新的csv文件时,有没有办法自动生成和更新津贴?因为津贴表本来应该是空的,实际上取决于考勤表
为此,您需要在MySQL中创建一个触发器,以便当新数据插入考勤表时,相同的查询将自动生成并运行津贴表格。
DROP TRIGGER IF EXISTS allowance_insert_trigger;
DELIMITER $$
CREATE TRIGGER allowance_insert_trigger
AFTER INSERT ON attendance FOR EACH ROW
begin
DECLARE days_attandence varchar(10);
SELECT count(*) INTO days_attandence FROM attendance WHERE emp_code=NEW.emp_code GROUP BY date;
INSERT INTO `allowance` ( `emp_code`, `emp_name`, `days_attended`, `allowance`) VALUES ( NEW.emp_code, NEW.emp_name, days_attandence, '30.00');
END;
$$
DELIMITER ;
你想写这样的触发器,这里我已经放了静态值作为余量,但你可以把你自己计算的值放在那里。