Kurt Marshman
Kurt Marshman

Reputation: 215

PHP Write HTML Table Row to Webpage

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>";

Upvotes: 2

Views: 557

Answers (2)

Zerquix18
Zerquix18

Reputation: 769

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

Upvotes: 0

DampeS8N
DampeS8N

Reputation: 3621

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)

Upvotes: 1

Related Questions