lAZLf Posted December 15, 2009 Share Posted December 15, 2009 I'm sure you can, but how? I have a page that creates a new page for every feed on it. I want to be able to store a whole php/html code on a variable so it would be easier for me to write it to a file. I'm sure this is an easy to solve problem. If it matters, here's the code: <?php // Connect to the database server mysql_connect("localhost", "xxxxx", "xxxxx") or die(mysql_error()); // Open to the database mysql_select_db("annarbo1_Archive") or die(mysql_error()); // Select all records from the "Individual" table $result = mysql_query("SELECT * FROM Archive ORDER BY date DESC") or die(mysql_error()); session_start(); $user = $_SESSION['valid_user']; ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>ANN ARBOR UNCOVERED - your informational destination</title> <link href="Level2_Verdana_Forms.css" rel="stylesheet" type="text/css" /> <script src="hideorshow.js" type="text/javascript"> </script> </head> <body> <div align="center"> <div id="black"><div id="centerer"> <table id="slogantopnav"><tr><td id="valign"> <div id="menu"> <div id="links"> <a href="index.php" class="link"> HOME</a> <a href="#" class="link"> ANN ARBOR</a> <a href="#" class="link"> ABOUT</a> <a href="#" class="link"> LINKS</a> <a href="#" class="link"> CONTACT</a><br/> <a href="restaurants.php" class="link"> RESTAURANTS</a> <a href="clubs.php" class="link"> CLUBS</a> <a href="#" class="link"> SHOPS</a> <a href="#" class="link"> STUDENT MAGAZINES</a> </div> </div> </td><td id="login"> <? if (!isset($_SESSION["valid_user"])) { echo' <form action="login.php" method="post"> <table cellspacing="0"> <tr><td><input type="text" name="username"width="300" value="username"/></td></tr> <tr><td><input type="password" name="password"width="300" value="password"/></td></tr> <tr><td><input type="submit" value="login"/></td></tr> </table> </form>'; } else { echo"Welcome ".$user; echo "<br> <a href='logout.php' class='link' >logout</a>"; } ?> </td></tr></table> </div> </div> <table id="top" cellspacing="0"> <tr height="110"><td id="landscape"><a href="index.php"><img id="img" src="header.png" /></a></td></tr> </table> <table width="900" id="main" cellspacing="0"> <tr> <td height="212"> <div id="scroll"> <table id="table" cellspacing="0"> <? while($row=mysql_fetch_array($result)){ $content = "hi".$row['title']; $file_name= $row['title'].".php"; $fp = fopen ($file_name, "w"); fwrite ($fp, $content); fclose ($fp); echo' <tr> <td height="20" class="title">'.$row['title'].'</td><td class="title"><div align="right">'.$row['date'].' </div> </td> </tr> <tr> <td height="100" colspan="2" class="content">'.$row['content'].' </td> </tr> <tr><td> <a href="'.$file_name.'" class="link">[comments]</a> </td></tr>'; } ?> </table> </div> </td><td rowspan="2"id="sideadvert"></td> <tr> <td height="37" colspan="2"><div id="copyright" align="center">© Copyright Ittai Svidler 2009</div></td> </tr><tr><td id="footer" colspan="2"></td></tr> </table> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/185281-store-php-functions-and-html-in-one-variable/ Share on other sites More sharing options...
lAZLf Posted December 15, 2009 Author Share Posted December 15, 2009 Sorry if the question isn't too clear. How would I store the code above as a single variable? Quote Link to comment https://forums.phpfreaks.com/topic/185281-store-php-functions-and-html-in-one-variable/#findComment-978110 Share on other sites More sharing options...
Adam Posted December 16, 2009 Share Posted December 16, 2009 Not sure I know what you mean. What would you then do with this variable? You can't store PHP code to be run in that sense (unless you ran it through eval). Do you not mean a "function"? Quote Link to comment https://forums.phpfreaks.com/topic/185281-store-php-functions-and-html-in-one-variable/#findComment-978159 Share on other sites More sharing options...
thebadbad Posted December 16, 2009 Share Posted December 16, 2009 Not sure I know what you mean. What would you then do with this variable? The OP says he want to write it to a file. The easiest way to do this would be using the nowdoc syntax to specify the string, but that requires PHP 5.3.0. Any other method potentially involves a lot of escaping. Quote Link to comment https://forums.phpfreaks.com/topic/185281-store-php-functions-and-html-in-one-variable/#findComment-978177 Share on other sites More sharing options...
lAZLf Posted December 16, 2009 Author Share Posted December 16, 2009 Not sure I know what you mean. What would you then do with this variable? The OP says he want to write it to a file. The easiest way to do this would be using the nowdoc syntax to specify the string, but that requires PHP 5.3.0. Any other method potentially involves a lot of escaping. how do you escape strings? As far as I know I haven't ever done it. Quote Link to comment https://forums.phpfreaks.com/topic/185281-store-php-functions-and-html-in-one-variable/#findComment-978187 Share on other sites More sharing options...
trq Posted December 16, 2009 Share Posted December 16, 2009 I want to be able to store a whole php/html code on a variable so it would be easier for me to write it to a file. Why do you want to do that? Quote Link to comment https://forums.phpfreaks.com/topic/185281-store-php-functions-and-html-in-one-variable/#findComment-978191 Share on other sites More sharing options...
lAZLf Posted December 16, 2009 Author Share Posted December 16, 2009 I want to be able to store a whole php/html code on a variable so it would be easier for me to write it to a file. Why do you want to do that? If you read the code I posted, It is writing/editing files. I want to be able to write a page similar to the current one, but of course I will change it a bit. I figured it would make more sense to use a variable in the fwrite(); function. Quote Link to comment https://forums.phpfreaks.com/topic/185281-store-php-functions-and-html-in-one-variable/#findComment-978193 Share on other sites More sharing options...
trq Posted December 16, 2009 Share Posted December 16, 2009 I want to be able to store a whole php/html code on a variable so it would be easier for me to write it to a file. Why do you want to do that? If you read the code I posted, It is writing/editing files. I want to be able to write a page similar to the current one, but of course I will change it a bit. I figured it would make more sense to use a variable in the fwrite(); function. I'm just not sure of why you would ever need multiple copies of the same (or similar) pages. Quote Link to comment https://forums.phpfreaks.com/topic/185281-store-php-functions-and-html-in-one-variable/#findComment-978196 Share on other sites More sharing options...
lAZLf Posted December 16, 2009 Author Share Posted December 16, 2009 It won't be the same, it will get information via database. If you want to see the code in action so far go to http://www.annarboruncovered.com/ You'll see that with the "while" loop it takes all the information out of the database, and displays something different in each row of the table. The same concept will be with the new pages, but they will only display the "feed" i post per page, where then I will make it so that users can comment on them. Quote Link to comment https://forums.phpfreaks.com/topic/185281-store-php-functions-and-html-in-one-variable/#findComment-978199 Share on other sites More sharing options...
trq Posted December 16, 2009 Share Posted December 16, 2009 It won't be the same, it will get information via database. If you want to see the code in action so far go to http://www.annarboruncovered.com/ You'll see that with the "while" loop it takes all the information out of the database, and displays something different in each row of the table. The same concept will be with the new pages, but they will only display the "feed" i post per page, where then I will make it so that users can comment on them. So why exactly do you need to create news scripts for this? Quote Link to comment https://forums.phpfreaks.com/topic/185281-store-php-functions-and-html-in-one-variable/#findComment-978201 Share on other sites More sharing options...
lAZLf Posted December 16, 2009 Author Share Posted December 16, 2009 Because I will be uploading things to the database ALL THE TIME. I'm most certainly not going to take the time to manually create a new file for each and every entry. Quote Link to comment https://forums.phpfreaks.com/topic/185281-store-php-functions-and-html-in-one-variable/#findComment-978202 Share on other sites More sharing options...
trq Posted December 16, 2009 Share Posted December 16, 2009 Because I will be uploading things to the database ALL THE TIME. I'm most certainly not going to take the time to manually create a new file for each and every entry. I think your missing the entire point of dynamic pages. Why do you even need multiple scripts? Do you think every time someone posts a new thread on this forum that a new file is created? That's not how dynamic web applications work. Quote Link to comment https://forums.phpfreaks.com/topic/185281-store-php-functions-and-html-in-one-variable/#findComment-978203 Share on other sites More sharing options...
lAZLf Posted December 16, 2009 Author Share Posted December 16, 2009 Then how do they work? Quote Link to comment https://forums.phpfreaks.com/topic/185281-store-php-functions-and-html-in-one-variable/#findComment-978204 Share on other sites More sharing options...
trq Posted December 16, 2009 Share Posted December 16, 2009 Ok, this example isn't related to what you are doing but the principles are the same. http://www.phpfreaks.com/forums/index.php/topic,161442.msg706139.html#msg706139 This is an example of how to write one script capable of displaying all users of a website's profile pages. Notice how you can pass arguments to the script via the url and have it display different data accordingly? Quote Link to comment https://forums.phpfreaks.com/topic/185281-store-php-functions-and-html-in-one-variable/#findComment-978207 Share on other sites More sharing options...
lAZLf Posted December 16, 2009 Author Share Posted December 16, 2009 Thanks thorpe, I got it. Quote Link to comment https://forums.phpfreaks.com/topic/185281-store-php-functions-and-html-in-one-variable/#findComment-978209 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.