/    Sign up×
Community /Pin to ProfileBookmark

php for loop with limit

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.

thanks.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 15.2011 — One approach:
[code=php]
$counter = 1;
while(($line = fgetcsv($fh)) !== false) {
// process this record
if(($counter++ % 200) == 0) {
sleep(120);
}
}
[/code]

Note that you'll probably need to modify the PHP timeout limit.
Copy linkTweet thisAlerts:
@praganauthorNov 15.2011 — 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
[code=php]
foreach ($csv->data as $key => $row){
foreach ($row as $value) {
$output = "$value, ";
echo $output;
}
}[/code]



Now , I want to add that limit for 100 records, sleep and continue until the end .
Copy linkTweet thisAlerts:
@NogDogNov 15.2011 — In my first reply, the check was done via the [b]$count % 200[/b] (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().
Copy linkTweet thisAlerts:
@praganauthorNov 15.2011 — hi, thanks, i tried that way with 10 records, so had the % 2 . But it's doing only for the records like only for even rows.

Not sure if I did wrong.

regards
Copy linkTweet thisAlerts:
@BelrickNov 17.2011 — Just curious

But what is the point of using sleep? Is it to delay the user presumably so they buy a premium none sleep version lol.
×

Success!

Help @pragan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...