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
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 .  '?>';

}

Link to comment
Share on other sites

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');

?>

Link to comment
Share on other sites

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);
}
?>

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.