cmzone Posted August 13, 2008 Share Posted August 13, 2008 I want to write this massive function that adds the product and creates a new page for my site. The top echo part works great but where I say "starting here" is where I need help. First I need to create the html page with the code, but then if the page exists I want it to do nothing at all. How would I go about doing this? <?php function newMonitor(array $model) { //Works Perfect echo '<div class="section"> <div class="images"> <img class="logo" src="images/logos/'.$model[1].'.png" alt="'.$model[1].'" /> <img class="monitor" src="images/'.$model[1].'/'.$model[0].'.png" alt="'.$model[1].' '.$model[0].'" /> </div> <div class="description"> <a href="#">'.$model[1].' '.$model[0].' '.$model[4].'" LCD Monitor</a> <ul> <li><strong>Color:</strong> '.$model[3].'</li> <li><strong>Size:</strong> '.$model[4].'</li> <li><strong>Resolution:</strong> '.$model[5].'</li> <li><strong>Response Time:</strong> '.$model[6].'ms</li> <li><strong>Input:</strong> '.$model[7].'</li> <li><strong>Brightness:</strong> '.$model[8].'cd/m2</li> <li><strong>Contrast Ratio:</strong> '.$model[9].'</li> <li><strong>Viewing Angle:</strong> '.$model[10].' (H/V) Degrees</li> <li><strong>Pixel Pitch:</strong> '.$model[11].'mm</li> </ul> </div> <div class="revieworbuy"> <a class="rr" href="'.$model[1].'-'.$model.'.html"></a> <a class="bn" href="'.$model[12].'"></a> <p>$'.$model[2].'</p> </div> <div class="clear"></div> </div>'; //STARTING HERE $myFile = "'.$model[1].$model[0].'.html"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <?php include("parts/head.php"); ?> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/jquery.cycle.js"></script> <script type="text/javascript" src="js/slider.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".featured").cycle({ fx: 'fade', speed: 300, timeout: 4000, pause: 1 , next: ".right", prev: ".left" }); }); </script> <title>New Page</title> </head> <body> <?php include("parts/header.php"); ?> <?php include("parts/slider.php"); ?> <?php include("parts/footer.php"); ?> </body> </html>'; fwrite($fh, $stringData); fclose($fh); } ?> Link to comment https://forums.phpfreaks.com/topic/119417-am-i-dreaming-with-this-file-write/ Share on other sites More sharing options...
flyhoney Posted August 13, 2008 Share Posted August 13, 2008 Maybe you should use a database and make a single page that displays products dynamically? Link to comment https://forums.phpfreaks.com/topic/119417-am-i-dreaming-with-this-file-write/#findComment-615194 Share on other sites More sharing options...
cmzone Posted August 13, 2008 Author Share Posted August 13, 2008 If I can echo a bunch of code shouldnt I be able to create a file with a bunch of code of my choice? Link to comment https://forums.phpfreaks.com/topic/119417-am-i-dreaming-with-this-file-write/#findComment-615197 Share on other sites More sharing options...
trq Posted August 13, 2008 Share Posted August 13, 2008 If I can echo a bunch of code shouldnt I be able to create a file with a bunch of code of my choice? Yeah but it kind of defeats the pupose of creating 'dynamic' websites. Anyway, I suggest most of your problem lies with the fact that you are not assigning a string to $stringData. Look closer at your code. As for doing nothing if the file already exists, check out file_exists(). Link to comment https://forums.phpfreaks.com/topic/119417-am-i-dreaming-with-this-file-write/#findComment-615199 Share on other sites More sharing options...
genericnumber1 Posted August 13, 2008 Share Posted August 13, 2008 Without applying it to your current code, I'm sure something simple like this is what you're looking for... <?php $content = 'mypagecontent'; $file = 'myfile.htm'; if(!file_exists($file)) { file_put_contents($file, $content); } ?> Link to comment https://forums.phpfreaks.com/topic/119417-am-i-dreaming-with-this-file-write/#findComment-615201 Share on other sites More sharing options...
cmzone Posted August 13, 2008 Author Share Posted August 13, 2008 so this will create the file if it doesnt exist? function newMonitor(array $model) { $content = ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <?php include("parts/head.php"); ?> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/jquery.cycle.js"></script> <script type="text/javascript" src="js/slider.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".featured").cycle({ fx: 'fade', speed: 300, timeout: 4000, pause: 1 , next: ".right", prev: ".left" }); }); </script> <title>Computer Monitors Zone | Top Rated Flat Screen Monitors</title> </head> <body> <?php include("parts/header.php"); ?> <?php include("parts/slider.php"); ?> <?php include("parts/footer.php"); ?> </body> </html>' '; $file = 'myfile.html'; if(!file_exists($file)) { file_put_contents($file, $content); } ?> Link to comment https://forums.phpfreaks.com/topic/119417-am-i-dreaming-with-this-file-write/#findComment-615204 Share on other sites More sharing options...
trq Posted August 13, 2008 Share Posted August 13, 2008 No because it still has errors in it. Single quotes within a single quoted string need be escaped. So this... fx: 'fade', would need be.... fx: \'fade\', Also, you might want to save the file with the .php extension if you plan on having php within it. Other then that.... are you getting any errors? Link to comment https://forums.phpfreaks.com/topic/119417-am-i-dreaming-with-this-file-write/#findComment-615235 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.