views:

55

answers:

3

I have blocks of HTML code in a MySQL database and my framework needs to print these within a PHP template which will be outputted to the browser. To do so I make this call:

</tr>
    <!-- Section 3 -->
    <?php echo SIN_SiteView::get('section3') ?>
<tr>

Which gets the code either from the APC or MySQL, now the code it obtains looks like this:

<td height="280" colspan="2" bgcolor="#00abd2">
<a href="#">
    <img src="<?php echo SIN_Utilities::l("image", "home_flash.png")?>" width="710" height="280" border="0" />
</a>

As you can see I need to run all images through a method known as "l" which I use to easily change images paths. Now the issue is if I echo that block of code it will simply be echoed as a string and not work.

I tried surrounding the php with '. [code] .' and removing the php but that also did not work. Does anyone have any ideas on how I could properly echo this to the page.

Thanks.

UPDATE: I think I need to be using the eval() command thanks to some of the comments, I simply do not understand how to implement it in my situation. Any simple examples would be greatly appreciated, for example how do I change this line:

<?php echo SIN_SiteView::get('section3') ?>

To echo the entire block featured above, thanks again.

+3  A: 

I think you want eval rather than echo. See this slightly different question.

My solution would be to eval '?>'.$myhtml.'<?php'.

Borealid
A: 

You'll need to use eval to evaluate the inline PHP. However, this is potentially quite risky (eval is evil, etc.), especially if any of the content that's being fetched is user sourced.

e.g.: At the very least, what's the stop the user inlining...

<?php die(); ?>

...within the content they enter.

As such, you'll need to take a great deal of care, if there's really no alternative to this approach.

Some updates:

  1. If you're new to PHP I'd recommend having a re-think. Chances are there's no need to use eval. (Unless there's a dynamically customised content on a per-user basis then you don't need it.) What are you trying to achieve?

  2. What specific error/problem are you having? (I presume you're using var_dump or print_r for debug purposes, etc.) As the content you need to eval isn't pure PHP (it's HTML with PHP in) you'll need to embed the PHP close and (re-)open tags as @Borealid illustrated.

middaparka
I am building a very simple CMS system for the marketing team, they are the only ones with access to what is in the DB and I doubt they would ruin their own site. :)
Alex
There's always an alternative. One of the million-and-a-half templating packages out there, for example.
cHao
@Alex - Put it this way, eval is an occasionally necessary evil - as long as you're aware of the trouble it could cause then at least you know what you're getting into. :-)
middaparka
It seems to be what I need I simply am having trouble understanding how to implement it in my code (sorry I am very new to PHP)
Alex
A: 

Is the marketing team adding the php code to the html you are storing?

If not, maybe you could change your <?php echo FUNCTION() ?> into @FUNCTION() and evolve your SIN_SiteView::get() into your own templating interpreter?

I agree with cHao though; it would probably be easier to adopt one of the templating packages out there and convert your data over.

KMW
As an intern I think the reason they are making me write this by hand is to complete the project but also as a learning experience I think using a pre-made package would be way easier (I could even use a pre-built CMS too) but building this by hand is kind of the point. And yes marketing team needs to be able to change the photos loaded by the php so yes they need to be able to control it
Alex