www.webdeveloper.com
Results 1 to 6 of 6

Thread: php for loop with limit

  1. #1
    Join Date
    May 2007
    Posts
    20

    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.
    Last edited by pragan; 11-15-2011 at 09:10 AM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    One approach:
    PHP Code:
    $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

    eBookworm.us

  3. #3
    Join Date
    May 2007
    Posts
    20
    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 .

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    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

    eBookworm.us

  5. #5
    Join Date
    May 2007
    Posts
    20
    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

  6. #6
    Join Date
    Apr 2010
    Posts
    227
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles