Jump to content

PHP within PHP?


nyc_fan

Recommended Posts

Hi,

I really hope one of you nice people will be able to help as i've searched and searched for the answer to this question, but i just can't figure it out!

I have a mysql database which contains the main content of pages for my website.

So, when i need to select a page, the script finds the right row in the table and outputs it to display for the user. To display the text that is retrieved from the database, i use this normal bit of code -

<?php echo($dbarray['page']);  ?>

This all works fine, and if i include HTML within the 'page' field of the database, this is output to the page, so i can make things bold etc.

However, i want to be able to use a PHP script within the data that is retrieved from the database. For example, as part of the page text, i want to be able to use the users name, which is set in the main document. However, when i use this code -

<?php echo($username);  ?>

as part of the page text that is saved in the database, nothing hapens - there's just white space on the page!

People have suggested i use this code instead -

<' . "? echo($username) ?" . '>';

however all this does is output the whole PHP code to the screen, and the code isn't actually processed?

I hope this all makes sense, and if anyone has the answer as to how i can get this to work, it would be greatly appreciated.

Thanks in advance guys!
Link to comment
https://forums.phpfreaks.com/topic/14202-php-within-php/
Share on other sites

When you echo something it is sent straight to the browser, code will not be excuted unless you [url=http://php.net/eval]eval[/url] it.
You could do this: have placeholders in the database pages like {user}, and then replace them with the real variable values like so:
[code]
$page  = str_replace('{user}',$username,$dbarray['page']);
echo ($page);
[/code]
Link to comment
https://forums.phpfreaks.com/topic/14202-php-within-php/#findComment-55701
Share on other sites

redarrow, thanks for your reply, but i think ShogunWarrior is right as i am pulling the code from a database where it is stored as text, assigning it to an array and then trying to execute it.

I don't think this is possible, but i have used the first reply from ShogunWarrior and have worked some magic and now i can do exactly what i want to do - and it seems even easier than i imaged (although there is a bit of work involved to start with!)
Link to comment
https://forums.phpfreaks.com/topic/14202-php-within-php/#findComment-55819
Share on other sites

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.