macattack Posted May 27, 2008 Share Posted May 27, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/107453-solved-my-code-wont-echo-my-variables/ Share on other sites More sharing options...
Schlo_50 Posted May 27, 2008 Share Posted May 27, 2008 Firstly, change: echo "<h2>".$post."</h2><br><br><h4>Published on ".$postedDate." by ".BLOG_AUTHOR."</h4>"; to echo "<h2>".$post."</h2><br><br><h4>Published on ".$postedDate." by ".$BLOG_AUTHOR."</h4>"; Quote Link to comment https://forums.phpfreaks.com/topic/107453-solved-my-code-wont-echo-my-variables/#findComment-550771 Share on other sites More sharing options...
macattack Posted May 27, 2008 Author Share Posted May 27, 2008 Thanks for the suggestion, but that wasn't the problem. BLOG_AUTHOR is defined in the functions.php file and displays correctly. It's the two variables ($post and $postedDate) that aren't displaying. Quote Link to comment https://forums.phpfreaks.com/topic/107453-solved-my-code-wont-echo-my-variables/#findComment-550774 Share on other sites More sharing options...
kev wood Posted May 27, 2008 Share Posted May 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/107453-solved-my-code-wont-echo-my-variables/#findComment-550777 Share on other sites More sharing options...
sasa Posted May 27, 2008 Share Posted May 27, 2008 error is somewhere before can we see 5 lines before ana 5 lies after <?php $post = "Write your post here"; $postedDate = "2008/05/27"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/107453-solved-my-code-wont-echo-my-variables/#findComment-550796 Share on other sites More sharing options...
mushroom Posted May 27, 2008 Share Posted May 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/107453-solved-my-code-wont-echo-my-variables/#findComment-550798 Share on other sites More sharing options...
macattack Posted May 27, 2008 Author Share Posted May 27, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/107453-solved-my-code-wont-echo-my-variables/#findComment-550810 Share on other sites More sharing options...
sasa Posted May 27, 2008 Share Posted May 27, 2008 try to change your function printBlog() that include your file, don't echo contest of file Quote Link to comment https://forums.phpfreaks.com/topic/107453-solved-my-code-wont-echo-my-variables/#findComment-550840 Share on other sites More sharing options...
mushroom Posted May 27, 2008 Share Posted May 27, 2008 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()" Quote Link to comment https://forums.phpfreaks.com/topic/107453-solved-my-code-wont-echo-my-variables/#findComment-550854 Share on other sites More sharing options...
macattack Posted May 27, 2008 Author Share Posted May 27, 2008 Thanks Mushroom, that's what the problem was! I cleaned up the code a bunch, since I don't need that function at all. Quote Link to comment https://forums.phpfreaks.com/topic/107453-solved-my-code-wont-echo-my-variables/#findComment-550867 Share on other sites More sharing options...
mushroom Posted May 27, 2008 Share Posted May 27, 2008 Your welcome! Quote Link to comment https://forums.phpfreaks.com/topic/107453-solved-my-code-wont-echo-my-variables/#findComment-550892 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.