gradually show php results

Discussion in 'PHP' started by stats, Oct 22, 2008.

  1. #1
    Hi

    i often write pages that show result as the calculation goes

    things like ..
    for ($i=1; $i<100; $i++) {
    // Do a heavy job which may take some seconds
    echo time("H:i:s") . "<br>";
    }
    
    PHP:
    sometimes when i call this page - it starts loading as execution goes (slowly trowing the results on in my browser), and sometimes it just waits for like 5 minute and then just kicks all the result on my screen at once .. and i have no idea why is it so .. i use the same browser on the same machine, still the way of output differs ..

    so .. how can i make my browser to show the results as soon as the server send them, and not wait for the server to send the whole page and then throw the whole thing on my screen at once

    for example, if my script does an echo every 2 second, i want that result to appear in my browser every 2 seconds .. and not my browser to wait for 200 seconds and then throw the whole thing on screen

    any idea ?
     
    stats, Oct 22, 2008 IP
  2. FredL

    FredL Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can use ajax in your case and update some div element.
     
    FredL, Oct 22, 2008 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    http://www.php.net/flush

    Past that, some browsers handle display diferently than others.

    For isntance Opera allows visitors to set the redraw time to "instantly" or "x seconds" and there's nothing you as a programmer can do to affect that. It exists to let the visitor restrict the processing power the browser uses to refresh the display.
    I believe Firefox has a similar setting with the default being about a second.
     
    joebert, Oct 22, 2008 IP
  4. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yep. Browsers are not actually made to handle scripts that take minutes to do something and give you updates every now and then. If it's only for you, you could make a logfile that shows you what it's doing
     
    Kyosys, Oct 22, 2008 IP