0

I'm using a package > http://csv.thephpleague.com/

But I'm getting a Undefined offset: 1 when uploading, although the CSV does upload and works this error is thrown which obviously I don't the client see or think there's a problem.

Here's the method in question:

public function postUpload()
{
    $file = Input::file('file');
    $name = time() . '-' . $file->getClientOriginalName();
    $moved = $file->move(public_path() . '/uploads/CSV', $name);

    DB::table('wc_program_1')->truncate();

    $csv = new Reader($moved);
    $csv->setOffset(0); //because we don't want to insert the header
    $nbInsert = $csv->each(function ($row) use (&$sth) {
        DB::table('wc_program_1')->insert(
            array(

                'Road' => $row[0],
                'Parish' => $row[1],
                'WorkType' => $row[2],
                'TrafficManagement' => $row[3],
                'Duration' => $row[4],
                'Start' => $row[5]
            )
        );
        return true;
    });

    return Redirect::to('admin/programmes')->with('flash_success', 'Upload completed & new data inserted.');
}

And here the table structure:

enter image description here

2 Answers 2

2

There must be more to the error? Undefined Offset: 1 in somefile.php on line #4323 or w/e

Anyways it references a missing index from an array.

The only array I see is $row so $row[1] does not exist.

If you just want to quickly patch it then I suggest this:

'Parish' => (isset($row[1]) ? $row[1] : ''),

It would probably be a good idea to do this for all of them such as:

'Road' => (isset($row[0]) ? $row[0] : ''),
Sign up to request clarification or add additional context in comments.

5 Comments

This is the line its showing 'Parish' => $row[1],
Yes I'm going to try that for each of them, i was just answering your question :)
Yes and that worked so i'm guessing it was an empty field in the CSV somewhere then so now that worked a treat thanks dude!
Yep no problem! If you want then you can suppress errors completely by placing error_reporting(0); at the beginning of the PHP file which I strongly recommend for a production environment and use E_ALL in a development environment.
Laravel pretty much does that for you anyhow! I just couldn't work why the error was been thrown. But hey thanks all the same for your help/advice.
0

I also came across this issue however I believe my issue may have been slightly different to yours.

Within my CSV I was uploading I hadn't set the column titles and had left blank fields so despite the offset being set it was still trying to parse the row.

In short, ensure that you don't leave blank cells in the header!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.