0

I have a database driven website (dynamic pages) but the content is stored in static files that are included in my pages. I want to scrap these text files and put the content in a database table, but I don't know how to deal with PHP code in the content.

For example, consider the following text:

<p>You can learn more at <a href="<?php echo $MySite1; ?>">this website</a>.</p>

$MySite1 might echo <a href="wwww.mysite1.com">, but if I put it into a database table, it just displays as plain text.

So does anyone know how I could modify a query like the one below so that PHP code in content stored in field AI. Article will display as PHP code? In other words, I'd like visitors to see www.mysite1.com, not <?php echo $MySite1; ?>.

Thanks.

  $Topics = mysql_fetch_assoc(mysql_query("SELECT ZT.N, ZT.URL, ZT.Title, ZT.Subtitle, ZT.Parent, ZT.Live, AI.URL, AI.Article
  FROM gz_topics ZT
  LEFT JOIN gz_articles_topics_intro AI ON AI.URL = ZT.URL
  WHERE ZT.URL = '$MyURL' AND ZT.Live = 1"));
2
  • Just one comment: bad practice. I wouldn' t put php-code in a database. There are almost no situation where this would be helpful. However you could use eval() but what you are going for is bad! Commented Jun 28, 2013 at 21:45
  • It sounds like you're looking for a content management system with template abilities. I suggest you find one of these, rather than this ad hoc solution. Commented Jun 28, 2013 at 21:48

2 Answers 2

1

You can use eval() function (http://nl.php.net/manual/en/function.eval.php). Very dangerous as is shown in that linked page.

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

2 Comments

Thanks for the tips. There is another option that I've used which isn't too much of a headache. I can simply replace <?php $EchoValue; ?> with $EchoValue; then use str_replace to replace it with a value... str_replace('www.mysite.com', '$EchoValue;', $Text); Based on the tips I've read on this page, I should stick with that, rather than put PHP code in a database. Thanks.
Maybe it would be a good idea for you to work with some light weight and fast template engine... It could make your life much easier... Check out "Haanga" for example...
1

If you planning to use str_replace... More elegant solution could be just saving your template with %s,and using printf with the arguments needed...

really depends on the usage...

1 Comment

Sorry, I'm not familiar with %s or printf. I found some information about the printf function @ w3schools.com/php/func_string_printf.asp but it actually looks far more complicated than my solution. However, I don't understand what "saving your template with %s" means, and that's probably the key to what I'm missing. Thanks.

Your Answer

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