开发者

Getting remainding lines in a CSV

开发者 https://www.devze.com 2023-04-06 11:12 出处:网络
I have a script here which will get all the lines in a csv and insert them into a database (FileMaker to be exact), and I want it to count to 100 lines, break the loop, then tell me the remainding lin

I have a script here which will get all the lines in a csv and insert them into a database (FileMaker to be exact), and I want it to count to 100 lines, break the loop, then tell me the remainding lines which are left. My code is below, Please see and any help would be greatly appreciated.

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


Just FYI guys, I've sorted it out now. Thanks for your time anyways!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号