Recipe 8.11. Flushing Output to the Browser


8.11.1. Problem

You want to force output to be sent to the browser. For example, before doing a slow database query, you want to give the user a status update.

8.11.2. Solution

Use flush( ), as in Example 8-29.

Flushing output to the browser

<?php print 'Finding identical snowflakes...'; flush(); $sth = $dbh->query(     'SELECT shape,COUNT(*) AS c FROM snowflakes GROUP BY shape HAVING c > 1'); ?>

8.11.3. Discussion

The flush( ) function sends all output that PHP has internally buffered to the web server, but the web server may have internal buffering of its own that delays when the data reaches the browser. Additionally, some browsers don't display data immediately upon receiving it, and some versions of Internet Explorer don't display a page until it has received at least 256 bytes. To force IE to display content, print blank spaces at the beginning of the page, as shown in Example 8-30.

Forcing IE to display content immediately

<?php print str_repeat(' ',300); print 'Finding identical snowflakes...'; flush(); $sth = $dbh->query(     'SELECT shape,COUNT(*) AS c FROM snowflakes GROUP BY shape HAVING c > 1'); ?>

8.11.4. See Also

Recipe 23.13; documentation on flush( ) at http://www.php.net/flush.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net