blargalarg Posted July 30, 2008 Share Posted July 30, 2008 Hi all. I used to use php pretty regularly in my spare time, but that was years ago and it seems like I've lost all my pertinent knowledge, so I'm asking for some help here. Please. Pretty please. I'm trying to make a php script that will speed up web page creation. The first page has several forms that pass data to the php script (name of the page, defining links, etc). I've remembered how to force php to insert HTML to a file, my current problem is that I need to insert some variables into the middle of the HTML repeatedly. ie: the file name occurs several times over the scope of the final webpage. I can't remember how to do this. Do I end the php and start again every time I need to call a previously used variable? I'm lost. Here's my code: <?php $suite = $_POST['suite'] ; $quad = $_POST['quad'] ; $floor = $_POST['floor'] ; $unit1 = $_POST['unit1'] ; $unit2 = $_POST['unit2'] ; $unit3 = $_POST['unit3'] ; $unit4 = $_POST['unit4'] ; $unit5 = $_POST['unit5'] ; $filename = "$suite.html"; $fh = fopen($filename, 'w') or die("can't open file"); $input = ' <html> <head> <title>Paradise Village Office Park</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" > <link rel="stylesheet" type="text/css" href="styles.css" > </head> <body> <div id="doc" class="yui-t6"> <div id="hd"> <div id="header"><h1> Paradise Village Office Park <br/> Plaza East Quadrant - Suite '; //Here is where I want to include the '$suite' variable again before sending more html to the page fwrite($fh, $input); fclose($fh); ?> Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/ Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 You're using single quotes, and variables don't work in single quotes. That could be your problem. Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/#findComment-603784 Share on other sites More sharing options...
blargalarg Posted July 30, 2008 Author Share Posted July 30, 2008 Thanks for the reply! When I insert it in double quotes it gives me an error: Parse error: syntax error, unexpected T_STRING in /home/parse.php on line 55 When I end it with a semicolon it fails to write the variable to the file. And when I include the variable in the fwrite, it makes the file but fails to write anything to it [ie: fwrite($fh, $input, $suite);] here's the code I used: <?php $suite = $_POST['suite'] ; $quad = $_POST['quad'] ; $floor = $_POST['floor'] ; $unit1 = $_POST['unit1'] ; $unit2 = $_POST['unit2'] ; $unit3 = $_POST['unit3'] ; $unit4 = $_POST['unit4'] ; $unit5 = $_POST['unit5'] ; $filename = "$suite.html"; $fh = fopen($filename, 'w') or die("can't open file"); $input = ' <html> <head> <title>Paradise Village Office Park</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" > <link rel="stylesheet" type="text/css" href="styles.css" > </head> <body> <div id="doc" class="yui-t6"> <div id="hd"> <div id="header"><h1> Paradise Village Office Park Plaza East Quadrant - Suite '; "$suite" fwrite($fh, $input); fclose($fh); ?> Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/#findComment-603792 Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 No no no. Put the whole string in double quotes and escape all of the other ". Or use sprintf() to get the values in. Or use single quotes to concatenate like this: $input = '<html> <head> <title>Paradise Village Office Park</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" > <link rel="stylesheet" type="text/css" href="styles.css" > </head> <body> <div id="doc" class="yui-t6"> <div id="hd"> <div id="header"><h1> Paradise Village Office Park Plaza East Quadrant - Suite ' . $suite ; Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/#findComment-603796 Share on other sites More sharing options...
sasa Posted July 30, 2008 Share Posted July 30, 2008 google for heredoc Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/#findComment-603806 Share on other sites More sharing options...
blargalarg Posted July 30, 2008 Author Share Posted July 30, 2008 Worked like a champ, DarkWater. Thank you very much for baby-stepping me through it. One final question: I'd like to add more html past that $suite variable. Do I need the period and then the new variable? ie: $input = '<html> <head> <title>Paradise Village Office Park</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" > <link rel="stylesheet" type="text/css" href="styles.css" > </head> <body> <div id="doc" class="yui-t6"> <div id="hd"> <div id="header"><h1> Paradise Village Office Park Plaza East Quadrant - Suite ' . $suite . $input2 = '<more html>' .; Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/#findComment-603809 Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 Sasa, heredocs are ugly. D: Anyway, do: $input = '<html> <head> <title>Paradise Village Office Park</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" > <link rel="stylesheet" type="text/css" href="styles.css" > </head> <body> <div id="doc" class="yui-t6"> <div id="hd"> <div id="header"><h1> Paradise Village Office Park Plaza East Quadrant - Suite ' . $suite . 'some more HTML <b>Blah blah blah</b>Lol'; Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/#findComment-603812 Share on other sites More sharing options...
revraz Posted July 30, 2008 Share Posted July 30, 2008 You could always just append to the variable too, may make it easier to keep track of, or use Double Quotes around the string with single quotes inside. Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/#findComment-603817 Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 There are tons of ways to do it. Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/#findComment-603818 Share on other sites More sharing options...
blargalarg Posted July 30, 2008 Author Share Posted July 30, 2008 You guys are champs. I appreciate all the help. DarkWater, your solution worked. I've run into another snag with the new bit of code I'm trying to insert. Is it because it's javascript? <?php $suite = $_POST['suite'] ; $quad = $_POST['quad'] ; $floor = $_POST['floor'] ; $unit1 = $_POST['unit1'] ; $unit2 = $_POST['unit2'] ; $unit3 = $_POST['unit3'] ; $unit4 = $_POST['unit4'] ; $unit5 = $_POST['unit5'] ; $filename = "$suite.html"; $fh = fopen($filename, 'w') or die("can't open file"); $input = '<html> <head> <title>Paradise Village Office Park</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" > <link rel="stylesheet" type="text/css" href="styles.css" > </head> <body> <div id="doc" class="yui-t6"> <div id="hd"> <div id="header"><h1> Paradise Village Office Park \<br> Plaza East Quadrant - Suite ' . $suite . ' <br/> Security Title <script language="JavaScript"> nvval = "<ILONWEB FUNC=ShowValue type=text SYMBOL=NVE_TS_1101 size=10></ILONWEB>"; if (nvval == "100.0 1"){ document.write('OCCUPIED');} else{ document.write('UNOCCUPIED');} </script> </h1> </div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="content"></br></a><img src="../../images/E-P111.jpg" width="500" height="418"></div> </div> </div> <div class="yui-b"> <div id="secondary"> <form method="GET" action=" '; fwrite($fh, $input); fclose($fh); ?> Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/#findComment-603824 Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 It's because of the ' inside of the javascript function calls. Change any ' to \' in the Javascript. Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/#findComment-603828 Share on other sites More sharing options...
blargalarg Posted July 30, 2008 Author Share Posted July 30, 2008 I don't suppose you live in Phoenix, DarkWater... I'd like to buy you a beer. Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/#findComment-603840 Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 Nope, New York. Too bad I can't buy a beer for myself on your behalf. Well actually there's this beer store in another town that supposedly does sell to minors (at least what I've heard) but whatever. =P Let's not get into that. Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/#findComment-603849 Share on other sites More sharing options...
unkwntech Posted July 30, 2008 Share Posted July 30, 2008 Coding PHP and CSS+(X)HTML: 5-6 years AND STILL A MINOR? OY, I'm going to be put out of business by my own children. Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/#findComment-603853 Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 Damn straight. xD Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/#findComment-603860 Share on other sites More sharing options...
blargalarg Posted July 30, 2008 Author Share Posted July 30, 2008 Get 'em, Dark! Thanks again for the help. It gave me the fundamentals I'd forgotten and has allowed me to troubleshoot the rest of my code. Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/#findComment-603867 Share on other sites More sharing options...
rhodesa Posted July 30, 2008 Share Posted July 30, 2008 the ob_* functions would make this a lot easier. then you don't have to worry about escaping characters: <?php $suite = $_POST['suite'] ; $quad = $_POST['quad'] ; $floor = $_POST['floor'] ; $unit1 = $_POST['unit1'] ; $unit2 = $_POST['unit2'] ; $unit3 = $_POST['unit3'] ; $unit4 = $_POST['unit4'] ; $unit5 = $_POST['unit5'] ; ob_start(); ?> <html> <head> <title>Paradise Village Office Park</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" > <link rel="stylesheet" type="text/css" href="styles.css" > </head> <body> <div id="doc" class="yui-t6"> <div id="hd"> <div id="header"><h1> Paradise Village Office Park \ Plaza East Quadrant - Suite <?php echo $suite; ?> Security Title <script language="JavaScript"> nvval = "<ILONWEB FUNC=ShowValue type=text SYMBOL=NVE_TS_1101 size=10></ILONWEB>"; if (nvval == "100.0 1"){ document.write('OCCUPIED');} else{ document.write('UNOCCUPIED');} </script> </h1> </div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="content"></br>[/url]<img src="../../images/E-P111.jpg" width="500" height="418"></div> </div> </div> <div class="yui-b"> <div id="secondary"> etc, etc, <?php $input = ob_get_clean(); $filename = "$suite.html"; $fh = fopen($filename, 'w') or die("can't open file"); fwrite($fh, $input); fclose($fh); ?> Quote Link to comment https://forums.phpfreaks.com/topic/117385-problem-with-calling-vairables-in-the-middle-of-html/#findComment-603873 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.