0

I have this issue. I have HTML code stored in the database. I read it, and I display it in my pages using echo $page_content;

I want to add somewhere in the middle of this HTML code a php include. Something like this: Inside this file I have also html code which is basically the country options for a select:

<option selected="selected" value="">Choose...</option>
<option value="AFGHANISTAN">AFGHANISTAN</option>
<option value="ALBANIA">ALBANIA</option>
<option value="ALGERIA">ALGERIA</option>
<option value="AMERICAN SAMOA">AMERICAN SAMOA</option>
<option value="ANDORRA">ANDORRA</option>
<option value="ANGOLA">ANGOLA</option>
etc etc

Until now I have hard-coded all the countries in many places, which I don't like.

But this is not displayed at all when i do my echo $page_content;

5
  • You want to bring the variable to global scope? Or what do you want? Commented Feb 19, 2013 at 15:18
  • How to add PHP code?, Can you show your code? Commented Feb 19, 2013 at 15:18
  • 1
    If you're storing PHP you want to execute in your database, then you're doing something wrong. I'd stop and rethink your design rather then trying to get this working. Commented Feb 19, 2013 at 15:20
  • Weh I'm tired. Oh well maybe I'll just keep this around.. comment: I suggest you use an absolute path in the include statement. For example you could define the root of your application using define( 'DIR_ROOT', dirname( __FILE__ ) ); in a file in the highest directory of your site, for example a config.php file (which has to be included/required by all other pages). Then you use this in your include calls. As such: include( DIR_ROOT . DIRECTORY_SEPARATOR . 'countries.php' ); Commented Feb 19, 2013 at 15:22
  • thanks to all for your suggestions. I know the architecture is terrible, but I inherited it from someone else and I am trying to make the best of it. Commented Feb 20, 2013 at 15:59

2 Answers 2

2

One of the easiest ways is to use tokens that get replaced with the content.

This can avoid the use of eval - which is not recommended ane would get me down voted by the herd.

For example:

HTML CODE

Hello, _TOKEN2_!

You can then use string replace functions to replace the TOKEN2 (the underscores are there but seemed to disappear here) with whatever you want:

str_replace("_TOKEN2_","World",$page_content);

And so on, you get the idea.

Sign up to request clarification or add additional context in comments.

Comments

-1

I would recommend you look into using something like Smarty to separate you're PHP and layout.

2 Comments

This is the correct way to do it... Any templating language and store it (the template) in DB.
Brian i see many articles about smarty templates etc and I don't know much. Can you give me an example please of how this applies in my case? basically i add {php} tags around my include, but still it shows nothing. Is there something I need to do fro smarty to work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.