I have 1000 records in a csv file, i am parsing it using php fgetcsv. Now I can get the record count and the data. Now i need to have a loop such that iterate through first 100 records, sleep/pause for 2 minutes, then continue from 101-200, 201-300 and so on until it reaches the end of all records.. then it stops. Please let me know if it's clear.
$counter = 1;
while(($line = fgetcsv($fh)) !== false) {
// process this record
if(($counter++ % 200) == 0) {
sleep(120);
}
}
Note that you'll probably need to modify the PHP timeout limit.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
hi, Thanks...but can you please let me know where this is checking to see if it process 100 records? and also how it will know to end if i don't check the total records count?
this is the code I have without this logic
PHP Code:
foreach ($csv->data as $key => $row){ foreach ($row as $value) { $output = "$value, "; echo $output; } }
Now , I want to add that limit for 100 records, sleep and continue until the end .
In my first reply, the check was done via the $count % 200 (which should have been 100 instead of 200).
If you prefer to do it in the foreach loop, it's the same thing: just initialize the $count variable, then use the same modulus math (%) in the if condition where you want to do the sleep().
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks