[英]Getting remainding lines in a CSV
我這里有一個腳本,它將把csv中的所有行插入到數據庫中(確切地說是FileMaker),我希望它計數到100行,打斷循環,然后告訴我剩下的剩余行。 我的代碼如下,請參閱,任何幫助將不勝感激。
public function insertTimesheet() {
//Get the public variables
$fm = $this->fm;
$timesheet = $this->data['timesheets'];
$total = count($timesheet);
$fm = $this->fm;
$count = 0;
$total = count($timesheet);
//Loop through the data items in the array
foreach ($timesheet as $element) {
//Checks each key value in the array
for ($i = 0; $i < count($timesheet); ++$i) {
if($i == 100){
printf('%d Timesheets added', $i);
break;
// Return which timesheets have not been added //
}
//Prevents duplicate entries
$count++;
//Manually add some data to the array
$timesheet[$i]['Created_By_Staff_ID'] = $_SESSION["user"]->staff_id;
$timesheet[$i]['Key_Staff_ID'] = $_SESSION["user"]->staff_id;
$timesheet[$i]['Timesheet_Type'] = 'CSV_UPLOAD';
$timesheet[$i]['Resource_ID'] = 'N/A At this time [CSV UPLOAD]';
//Convert the dates to USA dates (FileMaker Format)
$timesheet[$i]['Date_From'] = $this->convertDate($timesheet[$i]['Date_From']);
$timesheet[$i]['Date_To'] = $this->convertDate($timesheet[$i]['Date_To']);
//Insert the data into the Database
$addReq = & $fm->createRecord('Web_Staff_Timesheet', $timesheet[$i]);
$result = $addReq->commit();
//Checks for filemaker errors
if (!FileMaker::isError($result)) {
$return['error'] = false;
$return['msg'] = 'Timesheet added successfully';
$strOut = sprintf('<br />Working on %d of %d timesheets', $count, $total);
//return true;
} else {
$return['error'] = true;
$return['errorCode'] = $result->code;
$return['msg'] = 'Unable to add Timesheet';
$strOut = sprintf('<br />Sorry, we could not add your time sheet, FileMaker Produced this error: %s <br /> Failed to insert the following timesheets [Line number]: ', $return['errorCode'], $count);
return false;
}
}
break;
}
//Clear the array to prevent duplicates
unset($timesheet, $element, $this->data);
return $strOut;
}
僅供參考,我現在已經解決了。 無論如何,謝謝您的時間!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.