1

I've written the entire website in HTML and PHP but I ran into a small problem. After almost completing the website I was told to put all content in the database rather than just HTML. As I'm doing that I noticed that text with inline PHP variables doesn't get read when taken from the database.

Original Code:

<div>Hello <?php echo $World; ?></div>

New Code:

<div><?php getContent("title"); ?></div>

I have tried two ways of storing them in the database:
First method, text saved: Hello <?php echo $World; ?>
Which prints out Hello and ignores the variable.

Second method, text saved: Hello $World
Which prints out Hello $World instead of the value of $World

Other things I've tried:
Hello $$World - Hello $$World
Hello {$World} - Hello {$World}

10
  • 1
    So you think that every text taken from the database should be checked for variables presence? Really? Commented Nov 14, 2016 at 20:52
  • 2
    You can look into template packages like Smarty, or use placeholders and use str_replace to replace them. The much-maligned eval might also help you, but I cannot verify that. Commented Nov 14, 2016 at 20:57
  • 6
    omg, do not use eval! Commented Nov 14, 2016 at 20:58
  • 4
    "I was told to put all content in the database rather than just HTML" told by who? go tell them they are stupid. Commented Nov 14, 2016 at 20:58
  • 2
    You should probably be using some kind of template framework. You can't just put PHP code in a database. Commented Nov 14, 2016 at 21:06

2 Answers 2

1

You can use placeholders in your text, then use str_replace to put replace the placeholders with your variables.

DB String:

<div>Hello WORLD_VAR</div>

And in the code:

$text = str_replace('WORLD_VAR', $World, $text);
Sign up to request clarification or add additional context in comments.

Comments

1

Read about sprintf

echo sprintf("Hello %s",$World);

and store that "Hello %s" in the database and use $World like before.

If you implement it in the right way, you can make your website show in different languages.

But i thing you should more get into templating in php and/or use a template engine for php.

http://php.net/manual/de/function.sprintf.php

https://de.wikipedia.org/wiki/Template-Engine#Template-Engines_f.C3.BCr_PHP

And after the note in the comment:

The are many!! ways in php to do this Hello <?php echo $World; ?> But in the end you only store real data in the database. No php code or html just plain data. One of them can be templates like Hello %s other dynamic data like user names or whatever. And a template engine can help you to do that all in a proper way. (an templates are file-based, mostly) Keep that in mind.

3 Comments

But what about other variables? He wants to store ALL the PHP content into the database.
Sry, that i twice hit post and wasn't really finished. :)
This is nice but the problem is that when someone else would edit the content, someone without programming experience, they would not be able to add their own variables or modify existing ones through the database, they will need to access the html files and risk breaking the site.

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.