1

I have a file which I need to import into my database but I like to import 10 record at a time.

e.g. I have 100 records in my .csv file so first time it runs it will start from 0 then it will goto 10 and changes the your to domain.com/importfile.php/?start=10

this is the code I use.

$file = fopen($file, "r");
    while (($data = fgetcsv($file, 8000, ",")) !== FALSE) {
        $county = new County();
        $county->countrycode = $countrycode;
        $county->code = trim($data[0]);
        $county->var_name = $county->mod_write_check( trim($data[1]) );
        $county->name = trim($data[1]);
        $county->statecode = trim($data[2]);
        $save = $county->save();
    }
    fclose($file);

I would like to know if this can be done.

2
  • Parse your CSV file into an array. Cache it. Insert entries by 10. Commented May 27, 2013 at 16:11
  • if the csv file is over 100000 record it fails because it ust time out thats the reason i like to break the file in to 10 records process. Commented May 27, 2013 at 16:30

2 Answers 2

1

I would recommend using SplFileObject for deailing with files.

Here's a blog that goes thrugh the basics:

http://hakre.wordpress.com/2010/07/25/parsing-csv-files-with-php-spl-style/

The SplFileObject can easily seek but in combination with limitIterator you can also do:

$csv = new SplFileObject('data.csv');
$csv->setFlags(SplFileObject::READ_CSV);

foreach(new LimitIterator($csv, 0, 500) as $line){
  #save $line
}

Code snippet source

Sign up to request clarification or add additional context in comments.

3 Comments

I need to start from 1 to 10 first time then 11 to 20. this does start from when ever i wont but the end record his always same.
will this break if i wont to add a csv file which is over 400mb
how do I count number of records in this csv file.
1

You could use $seek = ftell($file); ftell documentation and fseek($file, $seek+1) you will need to carry in your session or somewhere else the value of $seek

2 Comments

i like to start at 0 when loop reach 10 then it refresh the page but this time it start from 11, i like to ignore the session to keep information.
You can have a variable just outside your loop that counts how many records you added and have a conditional and in the while loop. $inserted = 0 ; while(data = fgetcsv($file, 8000, ",")) !== FALSE && $inserted <= 10){ $inserted++ ; } $seek = ftell($file);

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.