【问题标题】:Automaticaly add new entry into a table codeigniter自动将新条目添加到表 codeigniter
【发布时间】:2018-08-03 16:27:41
【问题描述】:

我正在制定一个计划,根据每位员工的出勤率计算他们的膳食津贴。这是我现在拥有的数据,一个名为 attendance 的表。该表是从我导入的 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   |
----------------------------------------------------------------------------------

我需要计算他们参加了多少天并将其保存到我的数据库中。我已经有一个表作为容器,名为 allowance

------------------------------------------------------------------------
|id_allowance(auto increment)|emp_code|emp_name|days_attended|allowance|
------------------------------------------------------------------------
|          0001              |   brw  |  brown |       1     |  30.00  |
|          0002              |   cny  |  cony  |       1     |  30.00  |
------------------------------------------------------------------------

每次我导入新的 csv 文件时,有什么方法会自动生成和更新津贴?因为津贴表本来应该是空的,实际上取决于出勤表

【问题讨论】:

  • 这个问题与 PHP 或 CodeIgniter 无关。您可以通过自定义查询来执行此操作,以使用视图表获取所需的所有信息。
  • 我的错,很抱歉这个错误。是视图表是答案吗?因为我还需要我的数据库中的这些数据,而不仅仅是视图函数@Xartrick
  • 您想在每次导入attendance 时更新allowance
  • @Kisaragi 类似的东西,但主要问题是我不知道如何填写津贴,因为在我导入 csv 之前它应该是空的
  • 我没听懂你在说什么,听起来你可以只导入你的csv,然后select * from allowance where emp_code = value,如果没有结果则插入,如果有则更新。

标签: php mysql database codeigniter


【解决方案1】:

为此,您需要在 MySQL 中创建一个触发器,这样当新数据将插入到 attendance 表中时,同一查询将自动生成并为 allowance 表运行.

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 ;

你想写这样的触发器,这里我放了静态值作为津贴,但你可以放你自己的计算值。

【讨论】:

  • 公式是参加天数 * 一般津贴(我们从包含数字的表格中得到一般津贴)
  • 苏,你能帮帮我吗? ._. @Parth Viramgama
  • 是否可以让“参加天数”自动统计所选范围内的数据?
  • 对不起,我没听懂,请您简单解释一下吗?
  • 所以基本上我想计算每个员工参加了多少天,这需要我从 attendance 中计算数据并将其放入津贴中的“days_attended”。那么您可以在查询中添加一些内容吗?因为我不知道该怎么做._.
猜你喜欢
  • 2010-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-01
相关资源
最近更新 更多