Jump to content

Store php functions and html in one variable?


lAZLf

Recommended Posts

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

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.