Jump to content

Recommended Posts

I have a code that saves some variables to a php file. When I call up the file and ask it to echo those variables, I see nothing.

 

Here is the code to echo the variables:

echo "<h2>".$post."</h2><br><br><h4>Published on ".$postedDate." by ".BLOG_AUTHOR."</h4>";

 

Here is the HTML source code when the site is being displayed:

<h3>Just another test</h3><?php $post = "Write your post here"; $postedDate = "2008/05/27"; ?><h2></h2><br><br><h4>Published on  by Some guy</h4>

 

If you have a solution, that'd be great! Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/107453-solved-my-code-wont-echo-my-variables/
Share on other sites

try changing the variable $post to something else this might not do anything but it could also not like the name you have used as this is also used by php for the super and global variables as $_POST i no it not the same but it could help.

I have a code that saves some variables to a php file.

Just what are you saving?  (the value of the variable or the name and value of the variable.)

and how are you calling it?

if the variable is not being reset there will be nothing to echo.

 

That is the entire PHP file that is being saved, just the two variables and their values.

 

Here is how I call the function:

if($_GET['blog'] != ''){
	printBlog($_GET['blog']);
	echo "<h2>".$postedContent."</h2><br><br><h4>Published on ".$postedDate." by ".BLOG_AUTHOR."</h4>";
}

 

and here is the printBlog() function:

function printBlog($blog){
list($date) = sscanf($_GET['title'], "%s %d");
echo "<h3>".pathinfo($blog, PATHINFO_FILENAME)."</h3>";
$fp = fopen($blog, 'rb');
if(!($fp = fopen($blog, 'rb'))) echo "File failed to open";  
$data = fread($fp, filesize($blog));
fclose($fp);
echo $data;
}

 

I have just realised where the problem likely lies. The entry which contains the variables is showing up in the HTML source, php tags and all. It's saved as a .php file, so this shouldn't happen, should it?

 

Here is the code that catches the form data and prepares the file to be written:

$postContent = '<?php $postedContent = "'.stripslashes($_POST['content']).'"; $postedDate = "'.$_POST['date'].'"; ?>';

 

and then here is the code that actually writes the file

$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
$now = date(Ymd);
$posted = $year.$month.$day;

if($_POST['save'] == 'Save'){
	if(!is_dir(DIRECTORY_LISTING.'/'.$_POST['dir'])){
		mkdir(DIRECTORY_LISTING.'/'.$_POST['dir'], 0777, true);
	}
$fp = fopen(DIRECTORY_LISTING.'/'.$_POST['blog'].'.php', 'wb');
fwrite($fp, $postContent);
fclose($fp);
echo "Blog entry saved/updated<br><br>";
}

 

This part of the code is working, as the file is being created in the proper directory, it is only when I call up the entry and ask it to echo the two variables that nothing happens.

 

The reason I need it to save as variables (or so I think) is because when I want to edit the blog entry, I want the information to be visible in the form. This is just a simple blog, one of the applications of it will be using around 8 variables once it's up and running. I'm starting to wonder, however, if an array may be better?

I have just realised where the problem likely lies. The entry which contains the variables is showing up in the HTML source, php tags and all. It's saved as a .php file, so this shouldn't happen, should it?

That is because you are reading it like a text file.

you may be better off using "include()" rather than "fread()"

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.