Forum Moderators: coopster

Message Too Old, No Replies

PHP static page creation

         

cremesoda201

2:05 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



I'm creating a system that will build out numerous static pages from database content. Right now, I'm just getting the basics down and I have a test going. I found a script to build out pages, but the problem is: it outputs the results of ALL the pages made to the browser. Is there anyway to stop this? (There will be near 500-1000) pages being made in the end. The code below creates 5 test pages "0_test_page.htm" etc. with the html content in the middle. Here is my test coding:

<?PHP

for($i = 0; $i < 5; $i++)
{

?>

<?php
$page = $i . "_test_page.htm";
$file = 'blank.htm';
$newfile = $page;

if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>

<?php
ob_start(); // start the output buffer

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Test Generate Page <?PHP echo $i;?></title>
</head>

<body>

<P>This is a test page.</P>
<P>This is a test page.</P>
<a href="#">This is a test link.</a>

</body>
</html>
<?php
$cachefile = $page;
$fp = fopen($cachefile, 'w'); // open the cache file "cache/home.html" for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush(); // Send the output to the browser
?>

<?PHP }?>

mcibor

3:02 pm on Jun 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is easy: you write html in for loop

for(...)
{
?>
<html>
<?}?>

will write <html><html><html>....
Just put what you echo outside for loop and it should be all right!
Michal Cibor

cremesoda201

5:38 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



The HTML in there needs to be becuase it is being put on all the pages being created.

Philosopher

6:06 pm on Jun 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why not use ob_end_clean() instead of ob_end_flush?

From PHP.net

To output what is stored in the internal buffer, use ob_end_flush(). Alternatively, ob_end_clean() will silently discard the buffer contents.

cremesoda201

2:03 pm on Jun 8, 2005 (gmt 0)

10+ Year Member



Worked. Thanks a lot.