How do I force output to the screen during a while loop

Discussion in 'PHP' started by sm9ai, Feb 9, 2009.

  1. #1
    Hi All,

    I have a PHP script that runs in a while loop which takes about 10 minutes to run.

    I want to output updates to the screen whilst this is running but it waits until the script is finished then outputs it all in one go.

    Is there anyway to make it output as its going along?

    Thanks

    Matt
     
    sm9ai, Feb 9, 2009 IP
  2. steelaz

    steelaz Peon

    Messages:
    47
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't think it's possible. Server sends page to the browser after page is generated.

    I had similar problem with PHP task running almost an hour. To avoid timeouts and other problems, I would create new task request in database and initiate cron job to start the process. While working on the task, cron job would update status in database. When task request is created, I redirect browser to the page that is checking on task status every few seconds using AJAX.
     
    steelaz, Feb 9, 2009 IP
  3. mytor

    mytor Peon

    Messages:
    80
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    ob_start();
    while ($status == 'single') {
    echo 'having fun ... ';
    ob_end_flush();
    flush();
    $status = go_date();
    }
    
    PHP:
     
    mytor, Feb 9, 2009 IP
    steelaz and sm9ai like this.
  4. sm9ai

    sm9ai Active Member

    Messages:
    746
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Perfect, just what I needed. Many Thanks - Rep added.
     
    sm9ai, Feb 9, 2009 IP
  5. steelaz

    steelaz Peon

    Messages:
    47
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ah.. forgot about output buffers. Thanks, mytor, good stuff.
     
    steelaz, Feb 9, 2009 IP
  6. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #6
    Just insert
    ob_implicit_flush(true);
    PHP:
    in the beginning of the script..
     
    wmtips, Feb 9, 2009 IP
    steelaz likes this.