Jump to content

stumped with my php code.


persian-rice

Recommended Posts

Hello. I'm new to php and have read PHP and mySQL by Michael Davis almost cover to cover, so I have the fundamentals. This is my first practical code that I am trying and I have hit a roadblock for the last 7 hours and I'm pretty much stumped. Sorry if my code is messy, this is a symplification of what I have.

 

Right now nothing happens, even though I can't see why not, but the goal is to require a php page that takes code from a text file, puts them into an array and then echo the code using a loop. The function, which is a page layout, is required at the top of the index.php. Thanks for any help whatsoever, my brain is tired.

 

Main Document

index.php

<?php require('layout_function.php'); ?><!--load blog layout function-->
<html>
<head></head>
<body>
<?php require('make_blog_posts.php'); ?><!--call all posts-->
</body>
</html>

 

 

Blog Layout

layout_function.php

<?php
function blog_post ($blogTitle, $blogText, $blogDate)
{
echo '<div class="Title">'.$blogTitle.'</div>';
echo '<div class="Text">'.$blogText.'</div>';
echo '<div class="Date">'.$blogDate.'</div>';
}
?>

 

 

Load Blog Text

make_blog_posts.php

<?php
$blog_file = file_get_contents('blogtext.txt');
$blog_list = explode('~', $blog_file);
foreach ($blog_list as $value)
{
echo '<?php ' . $value . '?>';
}
?>

 

Text File

blogtext.txt

~

blog_post ('Title1', 'The Text1', 'Jan 1 09');

~

blog_post ('Title2', 'The Text2', 'Jan 2 09');

~

blog_post ('Title3', 'The Text3', 'Jan 3 09');

Link to comment
https://forums.phpfreaks.com/topic/143548-stumped-with-my-php-code/
Share on other sites

Just glancing through change this

 

<?php

$blog_file = file_get_contents('blogtext.txt');

$blog_list = explode('~', $blog_file);

foreach ($blog_list as $value)

{

echo '<?php ' . $value .  ?>';

}

?>

to

<?php

$blog_file = file_get_contents('blogtext.txt');

$blog_list = explode('~', $blog_file);

foreach ($blog_list as $value)

{

echo  $value ;

}

?>

or if u wnted the php tags

<?php

$blog_file = file_get_contents('blogtext.txt');

$blog_list = explode('~', $blog_file);

foreach ($blog_list as $value)

{

echo '<?php ' . $value .  '?>';

}

i need the php tags or it won't call the function. As for that quote before the php close tag, I have it there, it just isn't showing on the forum post. still no go.

 

Simply echoing '<?php' does not meen it will be executed as php.

 

I would suggest removing make_blog_posts.php all together and simply include the following from with a php script (eg; blogs.php)

 

<?php

blog_post ('Title1', 'The Text1', 'Jan 1 09');
blog_post ('Title2', 'The Text2', 'Jan 2 09');
blog_post ('Title3', 'The Text3', 'Jan 3 09');

?>

Found the Solution! eval() prints the values as php code instead of text. works like a charm now.

Thanks for the help nonetheless, the comment about php tags not echoing got me going in the right direction

 

<?php
$blog_file = file_get_contents('blogtext.txt');
$blog_list = explode('~', $blog_file);
foreach ($blog_list as $value)
{
eval ($value);
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.