开发者

Output loop one at a time rather than all at once?

I'm running PHP SOAP and the loop runs with 10 iterations. The issue I am having is that it will not output until the loop is complete, whereby displaying every output at once.

How can I have it output one at a time as it processes through the loop rather than all in one go after it completes the entire 10 loops?

<?php

$loops = 0; // set loops to 0

// connection credentials and settings
$location = 'https://theconsole.com/';
$wsdl = $location.'?wsdl';
$username = 'user';
$password = 'pass';

// include the console and client classes
include "class_console.php";
include "class_client.php";

// create a client resource / connection
$client = new Client($location, $wsdl, $username, $password);

while ($loops < 10)   
    {
    $dostuff;
    $echo "It has done: " .$stuff; // display output
 开发者_如何学运维   ob_end_flush(); ob_flush(); flush(); ob_start(); // added as per comments below - still not working
    }
?>

Thanks!


Try flush() command, also ob_flush() if dealing with output buffering

EDIT: Seems to be an issue with the sleep() command, if you read in the comments (even the dev ones) it indicates that if sleep is called within a script, nothing will be output until after it's run. If you loop sleep calls you have to wait till the end of the loop.

Read the comment "mohd at Bahrain dot Bz 16-Dec-2009 09:12" that method does work, but you have to fill the buffer each time :(.


Take a look at flush


You'll probably need to flush() and/or ob_flush() through each iteration if you want it to output each line as you get it.

Be advised, however, that this doesn't absolutely guarantee that you'll get each line as it becomes available, as teh web server may also be doing its own buffering too.


(Tested under IEx and Firefox)

<?php
if (ob_get_level() == 0) ob_start();

for ($i = 0; $i<10; $i++){
        echo "<br> Line to show.";
        echo str_pad('',4096)."\n";  

        ob_flush();
        flush();
}
echo "Done.";
ob_end_flush();
?>

Use this code, this is working for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜