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?