MySQL Question - Populating a variable inside of stored HTML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JoefromPhilly
Forum Newbie
Posts: 8
Joined: Sat May 31, 2008 3:36 pm

MySQL Question - Populating a variable inside of stored HTML

Post by JoefromPhilly »

Hi,

I have an HTML table stored in a MySQL table. Within the HTML, I want to pass a variable that is pulled out of another table. Here is a section of the content that is stored in the table:

<tr>
<td width="50%" height="21">
<p align="center"><font face="Arial" size="4">$m->business_name</font></td>
</tr>

I call the HTML table from MySQL using the following code:

$result = $db->query("SELECT html FROM offer_layout WHERE id='$id'");
$row = mysql_fetch_array($result);
$html = $row['html'];

To get the HTML table onto the web page, I do the following:

$mainContent = <<<EOD
$html
EOD;

Here is the problem - When the HTML table gets displayed, instead of seeing the business name and other business variables in the table, all I see are the variable names, such as "$m->business_name". How do I get the actual variable passed here?

I am storing the HTML table in a database table, as I have 4 different layouts that are used to either display on the web page, or to create the HTML table in an email that gets sent out.

Any advice?

- Joe
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: MySQL Question - Populating a variable inside of stored

Post by requinix »

Eww...
Do not put PHP code in a database. It's a slippery slope and it ends at massive security holes.

You can use templating instead. Same basic idea as to what you have now, except instead of PHP variables you have unique little strings. For example,

Code: Select all

<tr>
<td width="50%" height="21">
<p align="center"><font face="Arial" size="4">%BUSINESS NAME%</font></td>
</tr>
Then a simple str_replace to substitute "%BUSINESS NAME%" for $m->business_name.
Post Reply