Page 1 of 1

using a template system to return values from a table

Posted: Mon Aug 08, 2005 5:43 pm
by Burrito
I have a template system whereby I can put any field names from a table as such:

"This is a string stored on a MySQL database and my name is {%firstname%} and I am neat"

my goal is to be able to use that template so that I can query my database and have it return:

"This is a string stored on a MySQL database and my name is Burrito and I am neat"

here's how I'm doing it:

Code: Select all

$result = mysql_query("select * from nlms_leads where id = ".$_POST['id'])
	or die(mysql_error());
$row = mysql_fetch_assoc($result);
$gettemp = mysql_query("select * from nlms_user_template where id = ".$_POST['temp'])
	or die(mysql_error());
$gttemp = mysql_fetch_assoc($gettemp);
$pattern = "/\{%(\w*)%\}/";
$body = preg_replace($pattern,$row["$1"],$gttemp['body']);
where the leads table should grab the correct information (firstname=Burrito) and the user_template table should grab the template to be used.

Note: I have tried all of these derivations of my preg_replace all with no luck:

Code: Select all

$body = preg_replace($pattern,$row[$1],$gttemp['body']);
$body = preg_replace($pattern,$row[${"1"}],$gttemp['body']);
$body = preg_replace($pattern,$row[\\1],$gttemp['body']);
$body = preg_replace($pattern,$row["\\1"],$gttemp['body']);

Posted: Mon Aug 08, 2005 6:48 pm
by Burrito
I got it figured thx to an old buddy...thanks TD!

Code: Select all

$body = preg_replace($pattern,'$row["$1"]',$gttemp['body']);