2

I am trying to write a HTML table to a webpage row by row instead of having it all appear after the whole page has processed.

I have read about and attempted to add ob_flush(), flush(), ob_start(); ob_implicit_flush(true); ob_end_flush();

but everything I have attempted has resulted in the the whole table appearing all at once so I am not sure if it is possible misplacement of the code, misunderstanding of the use, or a setting on my server.

ob_start();
$url = "http://www.example.com";

$html = file_get_contents($url);
$doc = new DOMDocument();
$doc->loadHTML($html);

$tags = $doc->getElementsByTagName('img');
echo "<table>
<th>Path</th>
<th>Alt</th>
<th>Height</th>
<th>Width</th>";

foreach ($tags as $tag){
    $image = $tag->getAttribute('src');
    $alt = $tag->getAttribute('alt');
    $height = $tag->getAttribute('height');
    $width = $tag->getAttribute('width');

    echo "<tr>
    <td>$image</td>
    <td>$alt</td>
    <td>$height</td>
    <td>$width</td>
    </tr>";

    ob_flush();
    flush();
}

echo "</table><br>";
9
  • Please explain what you mean "row by row" Commented Nov 17, 2014 at 21:27
  • Did you add ob_start(); in the start of the file? Commented Nov 17, 2014 at 21:28
  • @Zerquix18 yes I did...sorry missing in original code. Added it. Commented Nov 17, 2014 at 21:31
  • @Nordenheim update an html table row when it is completed then the next row. Commented Nov 17, 2014 at 21:34
  • 1
    @Nordenheim Kurt is trying to send data early with flush() so that the table will show up bit by bit rather than all in one chunk after the processing is complete. This is desirable if you have an extremely long table as it gives the user some of the data without having to wait for all of it to render. Commented Nov 17, 2014 at 21:39

2 Answers 2

1

Flush can be interrupted by the web server you are using. Most commonly having GZIP turned on will cause the output to complete first before sending the whole thing in a compressed format. It could also be the server itself such as some older Windows servers.

You do not strictly speaking need the Output Buffer portion of your code. For what you are doing it is not needed. Flush() should be sufficient.

If you skip ahead in this tutorial to "Works with gzip" you can find ways to resolve your issue. (thanks jmbertucci)

Sign up to request clarification or add additional context in comments.

1 Comment

would it be possible to change the header of the page to chunk on the fly using php? I know you can set it when saving.
0

For example...

<?php 
ob_implicit_flush(true);
$buffer = "<br>";
echo "see this immediately.<br>";
echo $buffer;
ob_flush();
sleep(5);
echo "some time has passed";
?>

Thanks

2 Comments

I tried what you had suggested. I get the same results....page waits to load until table is complete.
I used to do it with an sleep function, you use "ob_start();" and then, in the function, you end the flush with ob_end_flush();, flush(); and then open ob_start(); again for the next call.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.