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?
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";
}