Jump to content

Using fopen to include a php template?!?


wright67uk

Recommended Posts

$query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='Restaurant' ORDER BY name");
echo mysql_error();
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];
$i = -1;
foreach($nt as $value)
{$i++;
echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".html'>"  . $nt[$i] . "</a>" . "<br/>";
$FileName = str_replace(' ','_',$nt[$i]) . ".html";
$FileHandle = fopen($FileName, 'w') or die("cant open file");}
fclose($FileHandle);
?>

 

When a page is opened using fopen i would like the file menu.php to be written to the file created.

To do this would I need to do somthing like;

 

$FileHandle = fopen($FileName, 'w', include ("menu.php")) or die("cant open file");}

 

Im not sure about the correct use of syntax here.  Any ideas?

Link to comment
Share on other sites

when fopen is functioned on a page that doesnt exist im creating a blank html page.

I basically want somthing for my users to look at.

I would like to have a template file such as the contents of menu.php or menu.html to be written to the newly created page.

 

any ideas welcome! Ive never really done anything like this before.

Link to comment
Share on other sites

You didn't really answer my question, so I'm just going to assume your menu.php doesn't actually use any php code, it's just html with a .php extension. Let's say you have a header.php, menu.php, and footer.php:

$pageContents = file_get_contents("header.php");
$pageContents .= file_get_contents("menu.php");
$pageContents .= file_get_contents("footer.php");
file_put_contents($Filename, $pageContents);

That'll put the contents of all three into the new file. No need to use fopen/fwrite/close if you're just going to write to it once.

Link to comment
Share on other sites

Thankyou, and yes you did presume right.

 

Ive uploaded header.php, menu.php and footer.php to the local directory.

 

my file code;

 

$query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='Restaurant' ORDER BY name");
echo mysql_error();
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];
$i = -1;
foreach($nt as $value)
{$i++;
echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".html'>"  . $nt[$i] . "</a>" . "<br/>";
$FileName = str_replace(' ','_',$nt[$i]) . ".html";
$FileHandle = fopen($FileName, 'w') or die("cant open file");}
$pageContents = file_get_contents("header.php");
$pageContents .= file_get_contents("menu.php");
$pageContents .= file_get_contents("footer.php");
file_put_contents($Filename, $pageContents); #LINE 38
fclose($FileHandle);

 

I get an error message;

Warning: file_put_contents() [function.file-put-contents]: Filename cannot be empty in D:\Hosting\####### on line 38

 

I also get this error with the below code;

 

$query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='Restaurant' ORDER BY name");
echo mysql_error();
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];
$i = -1;
foreach($nt as $value)
{$i++;
echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".html'>"  . $nt[$i] . "</a>" . "<br/>";
$FileName = str_replace(' ','_',$nt[$i]) . ".html";}
$pageContents = file_get_contents("header.php");
$pageContents .= file_get_contents("menu.php");
$pageContents .= file_get_contents("footer.php");
file_put_contents($Filename, $pageContents);

 

I cant really understand why!

Link to comment
Share on other sites

$query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='Restaurant' ORDER BY name");
echo mysql_error();
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];
$i = -1;
foreach($nt as $value)
{
$i++;
echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".html'>"  . $nt[$i] . "</a>" . "<br/>";
$FileName = str_replace(' ','_',$nt[$i]) . ".html";
$pageContents = file_get_contents("header.php");
$pageContents .= file_get_contents("menu.php");
$pageContents .= file_get_contents("footer.php");
file_put_contents($Filename, $pageContents);
}

You put my code outside of the foreach loop. Just because we give you code doesn't mean you can stick it anywhere and it'll work. You have to think about what it does and where it should go.

By the way, the whole purpose of the foreach() loop is to not have to keep a counter like $i going. You can just access $value and that'll have the value of the current array element. Like this:

foreach($nt as $value)
{
echo "<a href='" . str_replace(' ','_',$value) . ".html'>"  . $value . "</a>" . "<br/>";
$FileName = str_replace(' ','_',$value) . ".html";
$pageContents = file_get_contents("header.php");
$pageContents .= file_get_contents("menu.php");
$pageContents .= file_get_contents("footer.php");
file_put_contents($Filename, $pageContents);
}

Link to comment
Share on other sites

Thankyou for the reply,

 

I did to begin with try closing the loop at the end of the code as suggested in your code but it through up an error message for each page created.

 

Warning: file_put_contents() [function.file-put-contents]: Filename cannot be empty in D:\Hosting\3753557\html\1pw\b.php on line 37

 

I pressumed that as this error message was repeated for each page, then I must be doing somthing wrong with my loop.

Link to comment
Share on other sites

I got this to work in the end.

 

$query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='Restaurant' ORDER BY name");
echo mysql_error();
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];
$i = -1;
foreach($nt as $value)
{$i++;
echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".html'>"  . $nt[$i] . "</a>" . "<br/>";
$FileName = str_replace(' ','_',$nt[$i]) . ".html";
$FileHandle = fopen($FileName, 'w') or die("cant open file");
$pageContents = file_get_contents("header.php");
fwrite($FileHandle,"$pageContents");}
fclose($FileHandle);

 

Many thanks for the pointers, and code!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.