Dov
Dov

Reputation: 31

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!

Upvotes: 1

Views: 781

Answers (4)

user2753425
user2753425

Reputation: 301

(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.

Upvotes: 0

Viper_Sb
Viper_Sb

Reputation: 1817

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 :(.

Upvotes: 2

GordonM
GordonM

Reputation: 31730

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.

Upvotes: 0

chris
chris

Reputation: 4026

Take a look at flush

Upvotes: 1

Related Questions