Jump to content

echo message after each for loop run


severndigital

Recommended Posts

ok ..

 

so i want to have a message display to the browser after each instance of the for loop is complete. is there a way to do this in PHP or will i have to use Javascript to make it happen??

 

example

 

$arrayList = array(1,2,3,4,5);


for($i = 0;$i < count($arrayList); $i++){
      //do something here.
     // then echo completion information
     echo 'Process: ' . $arrayList[$i] . ' Complete <br />';
}

 

it works but shows all the echos after the script runs ... is there a way to echo the string directly after the loop runs ??

 

Thanks,

-C

 

 

 

 

Link to comment
Share on other sites

By default, PHP is buffered. You can force the output to be flushed to the browser with flush, however. Unless you're doing some pretty intensive stuff or you call sleep you're not going to notice it though.

 

Also, if you want to test it, you'll need to echo and flush some white space to the browser or something - a lot of browsers have their own buffering and require a certain amount of data before they'll display anything.

 

Edit: For example:

 

<?php
echo str_repeat(' ',256);
flush();
for($x=0;$x<5;$x++){
    echo $x.'<br />';
    flush();
    sleep(1);
}
?>

Link to comment
Share on other sites


$arrayList = array(1,2,3,4,5);


for($i = 0;$i < count($arrayList); $i++){
      //do something here.
     // then echo completion information
     echo 'Process: ' . $arrayList[$i] . ' Complete <br />';
}

ob_flush();
flush();

 

It will only work on some browsers. I know it works on FF, unsure about any others.

Link to comment
Share on other sites

It will only work on some browsers. I know it works on FF, unsure about any others.

 

Also, if you want to test it, you'll need to echo and flush some white space to the browser or something - a lot of browsers have their own buffering and require a certain amount of data before they'll display anything.

 

;)

Link to comment
Share on other sites

There is at least one more factor, if your server/browser negotiate to use compression to send the requested page.

 

Php, web servers, and browsers all (can) do buffering and compression. To get this to work you must statisfy/flush all the buffers and prevent any compression.

 

Web servers and browsers are not designed to have one request for a page to be output and displayed as discrete pieces.

 

The sure fire way to get this to work is to have the browser make timed requests to a page on the server and that page returns the available information that is then displayed in the browser. This is typically done using AJAX, but you could just have the page refresh itself at timed intervals.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.