wright67uk Posted April 10, 2011 Share Posted April 10, 2011 $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? Quote Link to comment Share on other sites More sharing options...
dcro2 Posted April 10, 2011 Share Posted April 10, 2011 Do you want to write out the contents of menu.php (with <?php tags and all), or what it would output if you included it? Quote Link to comment Share on other sites More sharing options...
wright67uk Posted April 10, 2011 Author Share Posted April 10, 2011 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. Quote Link to comment Share on other sites More sharing options...
dcro2 Posted April 10, 2011 Share Posted April 10, 2011 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. Quote Link to comment Share on other sites More sharing options...
wright67uk Posted April 10, 2011 Author Share Posted April 10, 2011 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! Quote Link to comment Share on other sites More sharing options...
dcro2 Posted April 10, 2011 Share Posted April 10, 2011 $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); } Quote Link to comment Share on other sites More sharing options...
wright67uk Posted April 11, 2011 Author Share Posted April 11, 2011 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. Quote Link to comment Share on other sites More sharing options...
wright67uk Posted April 11, 2011 Author Share Posted April 11, 2011 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.