自动向表codeigniter添加新条目

dsf9zpds  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(246)

我正在制定一个计划,根据每个员工的出勤情况计算他们的膳食津贴。这是我现在掌握的数据,一个叫做出勤率的表格。这个表格是由我导入的csv文件填充的,下面是我导入csv的代码

  1. function uploadData()
  2. {
  3. $fp = fopen($_FILES['userfile']['tmp_name'],'r') or die("can't open file");
  4. // Ignore header line
  5. $header = fgetcsv($fp);
  6. // Array to store the partial records
  7. $attendance= [];
  8. while($csv_line = fgetcsv($fp))
  9. {
  10. // Key to identify first part of record (Name and date)
  11. $key = $csv_line[1]."/".$csv_line[3];
  12. if ( isset($attendance[$key]) ){
  13. $start = $attendance[$key];
  14. // Extract data from first part of record and add in end date from current row
  15. $data = array(
  16. 'id_attend' => $start[0] ,
  17. 'emp_code' => $start[1],
  18. 'emp_name' => $start[2],
  19. 'date' => $start[3],
  20. 'time_in' => $start[4],
  21. 'time_out' => $csv_line[4],
  22. );
  23. $data['crane_features']=$this->db->insert('attendance', $data);
  24. // Remove partial record
  25. unset($attendance[$key]);
  26. }
  27. else {
  28. // Store partial record
  29. $attendance[$key] = $csv_line;
  30. }
  31. }
  32. fclose($fp) or die("can't close file");
  33. $data['success']="success";
  34. }

这是我的table

  1. | id_attend | emp_code | emp_name | date | time_in | time_out |
  2. ----------------------------------------------------------------------------------
  3. | 0001 | brw | brown |01.01.2001| 07.00 | 20.00 |
  4. | 0002 | cny | cony |01.01.2001| 07.00 | 20.00 |
  5. ----------------------------------------------------------------------------------

我需要计算他们参加了多少天,并把它保存在我的数据库里。我已经有一张table作为一个容器,叫做零用钱

  1. ------------------------------------------------------------------------
  2. |id_allowance(auto increment)|emp_code|emp_name|days_attended|allowance|
  3. ------------------------------------------------------------------------
  4. | 0001 | brw | brown | 1 | 30.00 |
  5. | 0002 | cny | cony | 1 | 30.00 |
  6. ------------------------------------------------------------------------

是否有任何方式的津贴将自动生成和更新每次我导入新的csv文件?因为津贴表本来应该是空的,真的要看考勤表

5cnsuln7

5cnsuln71#

为此,您需要在mysql中创建一个触发器,以便在考勤表中插入新数据时,自动生成并运行津贴表查询。

  1. DROP TRIGGER IF EXISTS allowance_insert_trigger;
  2. DELIMITER $$
  3. CREATE TRIGGER allowance_insert_trigger
  4. AFTER INSERT ON attendance FOR EACH ROW
  5. begin
  6. DECLARE days_attandence varchar(10);
  7. SELECT count(*) INTO days_attandence FROM attendance WHERE emp_code=NEW.emp_code GROUP BY date;
  8. INSERT INTO `allowance` ( `emp_code`, `emp_name`, `days_attended`, `allowance`) VALUES ( NEW.emp_code, NEW.emp_name, days_attandence, '30.00');
  9. END;
  10. $$
  11. DELIMITER ;

你想这样写触发器,这里我把静态值作为余量,但你可以把自己的计算值放在那里。

相关问题