Page 1 of 1

Is there any way to do this..

Posted: Fri Jan 02, 2009 11:16 pm
by throttle
Very undescriptive title, sorry, but its hard to sum up..

Anyways, I am creating a script that lets people create guestbooks for their sites and I am trying to make it completely customizable to the user by letting them control exactly how the messages are set up. They are able to do this through a textbox on an admin page which saves the code of the message to a mySQL table.
Here is a sample of the code for the message

Code: Select all

<table>
    <tr>
        <td>Name</td>
        <td>echo($name)</td>
    </tr>
    <tr>
        <td colspan='2'>
        <? echo($message) ?>
        </td>
    </tr>
</table>
So that gets stored to a mySQL column named 'message' and is called from the page that actually displays the message by

Code: Select all

echo($data['message']);
but even though other PHP works on the site, the php read from the mySQL table only show up on the site like text, not actual php code.

So my question is, is there any way to make it be read as php code, not just text?

Thanks

Re: Is there any way to do this..

Posted: Sat Jan 03, 2009 12:27 am
by requinix
Do it differently. (Yes, you can, but it's not the best strategy. It's outputted as-is because it's part of a string and PHP has no idea that there's supposed to be executable code in there.)

Use the typical templating methodology: instead of inserting PHP code into the mesage add placeholders. Something like

Code: Select all

<table>
    <tr>
        <td>Name</td>
        <td>[[NAME]]</td>
    </tr>
    <tr>
        <td colspan='2'>
        [[MESSAGE]]
        </td>
    </tr>
</table>
Then do a search-and-replace for each placeholder: replace "[[NAME]]" with $name, "[[MESSAGE]]" with $message, and so on.

It's a somewhat familiar (if not fairly obvious) syntax to non-coders and doesn't contain anything beyond what is necessary - ie, no <?php or ?> stuff anywhere.

Re: Is there any way to do this..

Posted: Sat Jan 03, 2009 1:11 am
by throttle
Wow, I can't believe I didn't think of that, I had completely forgotten about that bit of code. It works perfectly now

Thank you very much! You made it easier for users to customize too heh :)