我正在做一个小的约会项目。在我的CSV文件中,我有4列,分别是名称,员工ID,数据/时间,状态,在我的数据库列中是名称,empID,日期,timeIn,超时,状态。
我的csv文件的图片:(图片A)
这是我要保存在数据库中的内容:(图B)
示例输入CSV字符串:
Name,"Empoyee ID",Date/Time,Status
"Soriano, Jhoniel",901,"05/03/2019 1:01:03 PM",C/Out
"Soriano, Jhoniel",901,"05/04/2019 2:01:03 PM",C/In
"Soriano, Jhoniel",901,"05/04/2019 3:01:03 PM",C/Out
"Soriano, Jhoniel",901,"05/06/2019 4:01:03 PM",C/In
"Soriano, Jhoniel",901,"05/06/2019 5:01:03 PM",C/Out
"Soriano, Jhoniel",901,"05/07/2019 6:01:03 PM",C/In
有人可以给我一个肮脏的开端或任何想法来完成这项工作。
任何帮助都会受到赞赏。.
谢谢..
编辑:
我要感谢@jimmix给我一些入门的想法。
但是这是真实的情况,如果我不能很好地解释我的问题,很抱歉。
从我的CSV文件中,您可以获得在(图A)中可以找到的数据,然后使用表名“ tbldumpbio”将我的upload()函数上传到我的phpmyadmin数据库中,
请参阅下面的表结构:
从我的表tbldumpbio数据中,我有一个名为processTimesheet()的函数
代码如下:
public function processTimesheet(){
$this->load->model('dbquery');
$query = $this->db->query("SELECT * FROM tbldumpbio");
foreach ($query->result() as $row){
$dateTimeExplArr = explode(' ', $row->datetimex);
$dateStr = $dateTimeExplArr[0];
$timeStr = $dateTimeExplArr[1];
if($row->status='C/Out' and !isset($timeStr) || empty($timeStr) ){
$timeStrOut ='';
} else {
$timeStrOut = $dateTimeExplArr[1];
}
if($row->status='C/In' and !isset($timeStr) || empty($timeStr) ){
$timeStrIn ='';
} else {
$timeStrIn = $dateTimeExplArr[1];
}
$data = array(
'ID' => '',
'companyAccessID' => '',
'name' => $row->name,
'empCompID' => $row->empid,
'date' => $dateStr,
'timeIn' => $timeStrIn,
'timeOut' => $timeStrOut,
'status' => '',
'inputType' => ''
);
$this->dbquery->modInsertval('tblempbioupload',$data);
}
}
此功能将向tblempbioupload添加另一个数据。但是,这是我得到的结果:
请参阅以下数据:
问题是:
预期结果应该是这样的:
谢谢您,再次感谢您未能解决我的问题...:(
答案 0 :(得分:1)
不是最短的,但可以工作:
<?php
$csvMultilineStr =
'Name,"Empoyee ID",Date/Time,Status
"Soriano, Jhoniel",901,"05/03/2019 1:01:03 PM",C/Out
"Soriano, Jhoniel",901,"05/04/2019 2:01:03 PM",C/In
"Soriano, Jhoniel",901,"05/04/2019 3:01:03 PM",C/Out
"Soriano, Jhoniel",901,"05/06/2019 4:01:03 PM",C/In
"Soriano, Jhoniel",901,"05/06/2019 5:01:03 PM",C/Out
"Soriano, Jhoniel",901,"05/07/2019 6:01:03 PM",C/In';
//read CSV as every line = 1 array item
$csvArr = explode("\n", $csvMultilineStr);
//get header line as array of column names
//and remove that line from $csvArr
$headerArr = str_getcsv(array_shift($csvArr));
$emplIdKeyStr = "Empoyee ID";
$resultArr = [];
/**
* Create array of arrays
* that each array inside an array
* is under key of emploeeId and
* represents one CSV row
*
* [
* 901 => [
* 0 => [
* 'Name' => value
* 'Empoyee ID' => value2
* ...
* ],
* 1 => [
* 'Name' => value
* 'Empoyee ID' => value2
* ...
* ]
* ]
* ]
*
*/
foreach($csvArr as $csvLineStr) {
$csvEntryArr = str_getcsv($csvLineStr);
$csvLineArr = array_combine($headerArr, $csvEntryArr);
$emplIdInt = $csvLineArr[$emplIdKeyStr];
$resultArr[$emplIdInt][] = $csvLineArr;
}
var_export($resultArr);
/**
* Create array of arrays
* as above but with key => value
* structure
*
* [
* 901 => [
* '05/03/2019' => [
* 'C/In' => ...
* 'C/Out' => ...
* 'name' => ...
* ]
* ]
* ]
*
*/
$resultTimeIoArr = [];
$timeInOutColSwStr = 'Status';
$dateTimeColStr = 'Date/Time';
foreach($resultArr as $emplIdInt => $emplEntyArr) {
foreach($emplEntyArr as $emplSingleEntryArr) {
$dateTimeStr = $emplSingleEntryArr[$dateTimeColStr];
$dateTimeExplArr = explode(' ', $dateTimeStr);
$dateStr = $dateTimeExplArr[0];
$timeStr = $dateTimeExplArr[1];
$resultTimeIoArr[$emplIdInt][$dateStr][$emplSingleEntryArr[$timeInOutColSwStr]] = $timeStr;
$resultTimeIoArr[$emplIdInt][$dateStr]['name'] = '"' . $emplSingleEntryArr['Name'] . '"';
}
}
/**
* get sorted array by EmplId, Date, C/In or C/Out
* as an arry similar to first one CsvArr
*/
$resultCsvArr = [];
$resultCsvLineArr = [];
foreach($resultTimeIoArr as $emplIdInt => $singleEmplArr) {
foreach($singleEmplArr as $dateStr => $signleIoArr) {
$resultCsvLineArr['Name'] = $signleIoArr['name'];
$resultCsvLineArr['EmpID'] = $emplIdInt;
$resultCsvLineArr['Date'] = $dateStr;
if(!isset($signleIoArr['C/In']) || empty($signleIoArr['C/In'])) {
$timeIn = '';
} else {
$timeIn = $signleIoArr['C/In'];
}
if(!isset($signleIoArr['C/Out']) || empty($signleIoArr['C/Out'])) {
$timeOut = '';
} else {
$timeOut = $signleIoArr['C/Out'];
}
$resultCsvLineArr['timeIn'] = $timeIn;
$resultCsvLineArr['timeOut'] = $timeOut;
$resultCsvArr[] = $resultCsvLineArr;
}
}
echo "--- Array Result ---\n";
var_export($resultCsvArr);
//get header line as arry of column names
//from the keys of the first array
$head = array_keys($resultCsvArr[0]);
//put arr of header as 1st sting to csv string
$csvStr = implode(',',$head) . "\n";
//put all the rest in result sting
foreach($resultCsvArr as $resultEntryArr) {
$csvStr .= implode(',',$resultEntryArr) . "\n";
}
// file_put_contents('path-to-file', $csvStr);
echo "\n\n --- CSV Result ---\n";
print_r($csvStr);
给出输出:
---数组结果---
array (
0 =>
array (
'Name' => '"Soriano, Jhoniel"',
'EmpID' => 901,
'Date' => '05/03/2019',
'timeIn' => '',
'timeOut' => '1:01:03',
),
1 =>
array (
'Name' => '"Soriano, Jhoniel"',
'EmpID' => 901,
'Date' => '05/04/2019',
'timeIn' => '2:01:03',
'timeOut' => '3:01:03',
),
2 =>
array (
'Name' => '"Soriano, Jhoniel"',
'EmpID' => 901,
'Date' => '05/06/2019',
'timeIn' => '4:01:03',
'timeOut' => '5:01:03',
),
3 =>
array (
'Name' => '"Soriano, Jhoniel"',
'EmpID' => 901,
'Date' => '05/07/2019',
'timeIn' => '6:01:03',
'timeOut' => '',
),
)
--- CSV结果---
Name,EmpID,Date,timeIn,timeOut
"Soriano, Jhoniel",901,05/03/2019,,1:01:03
"Soriano, Jhoniel",901,05/04/2019,2:01:03,3:01:03
"Soriano, Jhoniel",901,05/06/2019,4:01:03,5:01:03
"Soriano, Jhoniel",901,05/07/2019,6:01:03,
使用:
file_put_contents('path-to-file', $csvStr);
最后将csv保存到文件。