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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘