duanqiao2225 2018-07-26 08:00
浏览 37
已采纳

CSV导入codeigniter与不同的数组

Currently doing a project that requires me to import a data from CSV file into database, here is the data sample of the file:

|   id_attendance   |   name   |   date   |  time   |
-----------------------------------------------------
|        001        |   lily   |01.01.2018|  07:00  |
|        002        |  thomas  |01.01.2018|  07:02  |
|        003        |   lily   |01.01.2018|  19:00  |
|        004        |  thomas  |01.01.2018|  19:02  |
-----------------------------------------------------

Meanwhile i need those data to be imported into this kind of table

|   id_attendance   |   name   |   date   |  time_in  |  time_out  |
--------------------------------------------------------------------
|        001        |   lily   |01.01.2018|  07:00    |   19.00    |
|        002        |  thomas  |01.01.2018|  07:02    |   19.02    |
--------------------------------------------------------------------

This requires me to automatically seperate the time fields into time_in and time_out. So far i already do the csv importing, here's my code

importcsv_m.php model

function importCSV()
{
    $count=0;
    $fp = fopen($_FILES['userfile']['tmp_name'],'r') or die("can't open file");
    while($csv_line = fgetcsv($fp,1024))
    {
        $count++;
        if($count == 1)
        {
            continue;
        }
        for($i = 0, $j = count($csv_line); $i < $j; $i++)
        {
            $insert_csv = array();
            $insert_csv['id_attendance'] = $csv_line[0];
            $insert_csv['name'] = $csv_line[1];
            $insert_csv['date'] = $csv_line[2];
            $insert_csv['time'] = $csv_line[3];
        }
        $i++;
        $data = array(
            'id_attendance' => $insert_csv['id_attendance'] ,
            'name' => $insert_csv['name'],
            'date' => $insert_csv['date'],
            'time' => $insert_csv['time'],
           );
        $data['crane_features']=$this->db->insert('attendance', $data);

    }       
    fclose($fp) or die("can't close file");
    $data['success']="success";
}

But its only importing the data without seperating the time (no time_in and time_out)

I was wondering if i can import an array and divide it into two?

  • 写回答

1条回答 默认 最新

  • dongtangu8615 2018-07-26 08:24
    关注

    I've created some code which tries to match the begining and end of each shift. This works by looking for a matching start record and then adds in data for the end time. If no matching start record is found, then it stores this data (assuming this is the start record). Once the end record is found, the start record is removed from $attendancies as the next record may be a new shift...

    function importCSV()
    {
        $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
        $attendancies = [];
        while($csv_line = fgetcsv($fp))
        {
            // Key to identify first part of record (Name and date)
            $key = $csv_line[1]."/".$csv_line[2];
            if ( isset($attendancies[$key]) ){
                $start = $attendancies[$key];
                // Extract data from first part of record and add in end date from current row
                $data = array(
                    'id_attendance' => $start[0] ,
                    'name' => $start[1],
                    'date' => $start[2],
                    'time_in' => $start[3],
                    'time_out' => $csv_line[3],
                );
                $data['crane_features']=$this->db->insert('attendance', $data);
                // Remove partial record
                unset($attendancies[$key]);
            }
            else    {
                // Store partial record
                $attendancies[$key] = $csv_line;
            }
        }
        fclose($fp) or die("can't close file");
        $data['success']="success";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作