I'm trying to echo after every iteration of a foreach loop in PHP. The echos aren't rendering until they are all done. I've tried the suggestions from various posts but none are working. Some of my code is:
echo $epTitle . "<br>";
echo "<div>" . '<img src="' . $imgSrc . '" height="70" width="70">';
echo "<button onclick='grabVid(" . '"' . $pageUrl . '"' ..")'>Grab</button>";
I've tried the following:
flush();
.
flush();
ob_flush();
.
while (ob_get_level() > 0)
ob_end_flush();
.
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
@ob_end_clean();
and other suggestions from: http://php.net/manual/en/function.flush.php and Echo 'string' while every long loop iteration (flush() not working)
Any suggestions? Thanks.
If you're using XHR (AJAX) and want to stream the response, you need to periodically check on it using onprogress
. The onreadystatechange
event will not fire whilst the response is being received, because the readyState
will always be loading
and not change until the request has completed.
Here is a good article on the subject:
PHP Streaming and Output Buffering explained
It contains this sample JavaScript for the XHR part:
xhr = new XMLHttpRequest();
xhr.open("GET", "response.php", true);
xhr.onprogress = function(e) {
alert(e.currentTarget.responseText);
}
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
console.log("Complete = " + xhr.responseText);
}
}
xhr.send();
(I don't know if there's any reason why the example uses e.currentTarget
instead of xhr
inside the onprogress
event handler - I just copied the example as it is.)
If you're planning on doing this loop for longer than 30 seconds, your PHP script will probably be killed. You can extend the default time limit but if you're site has many users, you want requests to be short as possible for optimum server availability.
This means rewriting your loop so that each iteration is a single HTTP request.