Jump to content

Am I dreaming with this file write?


cmzone

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.