Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Output Buffering With PHP
Use powerful filters to control the output of your PHP scripts.

| Today's Forecast |

As you saw on the previous page, you can display the contents of the current buffer at any time via a call to ob_end_flush(). If, however, you don't want to display the buffered output to the user, but instead want to do something else with it - write it to a file, for example - you have a couple of options.

The first - and simplest - involves using ob_get_contents() to extract the current contents of the output buffer to a PHP variable, and then processing that variable to get the results you desire. Here's a quick example:


<?php

// start buffering the output
ob_start();

// output format - either "www" or "file"
$output = "file";

// send some output
?>

<html>
<head><basefont face="Arial"></head>
<body>

<?
// open connection to database
$connection = mysql_connect("localhost", "joe", "nfg84m") or die ("Unable to connect!");
mysql_select_db("weather") or die ("Unable to select database!");

// get data
$query = "SELECT * FROM weather";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

// if a result is returned
if (mysql_num_rows($result) > 0)
{
// iterate through resultset
// print data
while (list($temp, $forecast) = mysql_fetch_row($result))
{
echo "Outside temperature is $temp";
echo "<br>";
echo "Forecast is $forecast";
echo "<p>";
}
}
else
{
echo "No data available";
}

// close database connection
mysql_close($connection);

// send some more output
?>

</body>
</html>

<?php

// now decide what to do with the buffered output
if ($output == "www")
{
// either print the contents of the buffer...
ob_end_flush();
}
else
{
// ... or write it to a file
$data = ob_get_contents();
$fp = fopen ("weather.html", "w");
fwrite($fp, $data);
fclose($fp);
ob_end_clean();
}

?>


In this case, the ob_get_contents() function is used to retrieve the current contents of the output buffer, and write it to a file.

Alternatively, you might want to use a callback function that is invoked every time output is caught by the buffer. The name of this user-defined callback function must be specified as an argument during the initial call to ob_start(), and the function itself must be constructed to accept the contents of the buffer as function argument.

If that sounded way too complicated, the next example, which rewrites the one above to use this technique, should make it clearer.


<?php

// user-defined output handler
function myOutputHandler($buf)
{
global $output;

// either dump the buffer to a file
if ($output != "www")
{
$fp = fopen ("weather.html", "w");
fwrite($fp, $buf);
fclose($fp);
}
// ... or return it for printing to the browser
else
{
return $buf;
}
}

// start buffering the output
// specify the callback function
ob_start("myOutputHandler");

// output format - either "www" or "file"
$output = "www";

// send some output
?>

<html>
<head><basefont face="Arial"></head>
<body>

<?
// open connection to database
$connection = mysql_connect("localhost", "joe", "nfg84m") or die ("Unable to connect!");
mysql_select_db("weather") or die ("Unable to select database!");

// get data
$query = "SELECT * FROM weather";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

// if a result is returned
if (mysql_num_rows($result) > 0)
{
// iterate through resultset
// print data
while (list($temp, $forecast) = mysql_fetch_row($result))
{
echo "Outside temperature is $temp";
echo "<br>";
echo "Forecast is $forecast";
echo "<p>";
}
}
else
{
echo "No data available";
}

// close database connection
mysql_close($connection);

// send some more output
?>

</body>
</html>

<?php

// end buffering
// this will invoke the user-defined callback
ob_end_flush();
?>


In this case, when ob_end_flush() is called, PHP will invoke the user-defined function myOutputHandler(), and will pass the entire contents of the buffer to it as a string. It is now up tp the function to decide what to do with the buffer - in this case, I've used the $output variable to decide whether or not to write it to a file. You can just as easily write a function to process it in some other way - for example, run a search-and-replace operation on the buffer, write it to a database, or email it to a specific address.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
Caching Web Sites With PEAR Cache
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback