将变量作为变量存储在数据库中


Storing variables as variables in database?

我在数据库中存储了一些电子邮件,因为它们是模板,我想让管理员能够编辑它们。所以有三列,initialmail,followup,followup2。然而,这些模板看起来是这样的:

Hello $Client, we are contacting you about your $product 

等等。在那之后,sendmail只会从右栏中随机选择一行并发送。但是,我不知道如何让管理员添加/编辑电子邮件,并在数据库中存储变量名而不是值,所以sendmail.php会知道有哪些变量,并用正确的值替换它们。请记住,这需要很容易,因为管理员会在现场编辑,所以我不能让他们每次想要在数据库中存储变量时都键入' .$Clientname. '

谢谢!

您可以这样做:

$replaceWord = array(
    "[name]" => $row['username'],
    "[age]" => $row['age'],
);
$emailContent = strtr($emailContentDatabase, $replaceWord); //notice $emailContentDatabase is what there is saved in the database.

例如,数据库中的电子邮件如下所示:

Welcome [name],
Your age is: [age].

然后使用上面的脚本将输出类似于这个的东西

Welcome Perry,
Your age is: 45.