PHP存储$obj->;输入mysql表以便在页面中使用
我试图使用preg_replace作为信函模板来生成具有动态内容的信函。我的概念是将任何对象引用的模式存储在数据库中,这样我就可以动态构建模式并替换阵列,而无需在页面中对它们进行“硬编码”。以下是我正在使用的表格和代码:PHP存储$obj->;输入mysql表以便在页面中使用,php,mysql,loops,pdo,Php,Mysql,Loops,Pdo,我试图使用preg_replace作为信函模板来生成具有动态内容的信函。我的概念是将任何对象引用的模式存储在数据库中,这样我就可以动态构建模式并替换阵列,而无需在页面中对它们进行“硬编码”。以下是我正在使用的表格和代码: **Table people id || first || last** 1 || john || smith 2 || mark || stone **Table address Id || people_id || mailing1** 1 || 1 || 123 fak
**Table people
id || first || last**
1 || john || smith
2 || mark || stone
**Table address
Id || people_id || mailing1**
1 || 1 || 123 fake street
2 || 2 || 34 melrose place
**Table macros
id || pattern || replace**
1 || first || $people->first
2 || last || $people->last
3 || mailing1 || $address->mailing1
//Set variables
$thePerson = $_POST['people_id'];
$theString = '<p>Dear {first} {last},</p><p>I am writing regarding your home at {mailing1}</p>';
$replacements = array();
$patterns = array();
$counter = 0;
//Get people information
$peopleSQL = $conn_read->prepare('SELECT * FROM people WHERE people.id=:people_id;');
$peopleSQL->execute(array(':people_id' => $thePerson));
$people = $peopleSQL->fetch(PDO::FETCH_OBJ);
//Get address information
$addressSQL = $conn_read->prepare('SELECT * FROM address WHERE address.people_id=:people_id;');
$peopleSQL->execute(array(':people_id' => $thePerson));
$people = $peopleSQL->fetch(PDO::FETCH_OBJ);
//Build macros for preg_replace
$macrosSQL = $conn_read->prepare('SELECT * FROM macros;');
$macrosSQL->execute();
while ($macros = $macrosSQL->fetch(PDO::FETCH_OBJ)) {
$patterns[$counter] = '/\{'.$macros->pattern.'\}/';
$replacements[$counter] = $$macros->replace;
$counter++;
}
echo preg_replace($patterns, $replacements, $theString);
**表格人物
id | |第一| |最后**
约翰·史密斯
2 | |标记| |石
**表地址
Id | | | | | |人| Id |邮件1**
1 | | 1 | | 123假街
梅尔罗斯广场2 | | 2 | | 34号
**表宏
id | |图案| |替换**
1 | |第一| |$人->第一
2 | | last | |$people->last
3 | |邮寄1 | |$地址->邮寄1
//设置变量
$thePerson=$\u POST['people\u id'];
$theString='亲爱的{first}{last},我写信是关于你在{mailing1}的家;
$replacements=array();
$patterns=array();
$counter=0;
//获取人员信息
$peopleSQL=$conn_read->prepare('SELECT*FROM people WHERE people.id=:people_id;');
$peopleSQL->execute(数组(':people_id'=>$thePerson));
$people=$peopleSQL->fetch(PDO::fetch_OBJ);
//获取地址信息
$addressSQL=$conn\u read->prepare('SELECT*FROM address WHERE address.people\u id=:people\u id;');
$peopleSQL->execute(数组(':people_id'=>$thePerson));
$people=$peopleSQL->fetch(PDO::fetch_OBJ);
//为preg_replace生成宏
$macrosSQL=$conn_read->prepare('SELECT*FROM macros;');
$macrosSQL->execute();
而($macros=$macrosSQL->fetch(PDO::fetch_OBJ)){
$patterns[$counter]='/\{.$macros->pattern.'\}/';
$replacements[$counter]=$$macros->replace;
$counter++;
}
echo preg_replace($patterns,$replacements,$theString);
对于输出,假设$thePerson设置为1。我从这段代码中得到的输出是:
“亲爱的$people->first$people->last
我在$address->mailing1上写了一封关于你家的信
其中所需输出为:
“亲爱的约翰·史密斯
我写的是关于你在fake street 123号的家”
我知道$macro->replace作为字符串从数据库中出来,并被视为字符串,这就是为什么我看到了我看到的结果,但我希望有一种方法来处理这个问题,或者一些语法可以给我期望的结果。我所做的研究一直在使用eval()函数,但我对它一点也不熟悉,每个人都说不要使用它
提前感谢你给我的指导,因为我不知所措。另外,如果有人有完全不同的方法来处理这个问题,我非常愿意接受建议。我建议您不应该在数据库中存储任何对象引用,因为您不需要它们:如果您不想进行任何“硬编码”,如果对象引用不会更改,为什么要存储它们我猜您不会每天在对象中存储完全不同的数据集,所以您可以在代码中进行修复 首先,您必须在一个数组中收集人员的所有数据,这对您来说应该不会太难。然后创建两个数组,一个用于搜索一个用于替换,并使用
str\u replace($search,$replace,$string)
,因为您知道$theString
中的每个键,并且知道应该插入哪些数据
$person = new person($_POST["people_id"]); // I don't know if you know this kind of coding (OOP)... but you could also do your MySQL-queries to create an array storing all data you need
$theString = "<p>Dear {first} {last},</p><p>I am writing regarding your home at {mailing1}</p>";
$search = array(
"{first}",
"{last}",
"{mailing1}",
);
$replace = array(
$person->first,
$person->last,
$person->mailing1,
);
echo str_replace($search, $replace, $theString);
$person=新人($\u POST[“people\u id”]);//我不知道你是否知道这种编码(OOP)。。。但您也可以通过MySQL查询创建一个数组来存储所有需要的数据
$theString=“亲爱的{first}{last},我写的是关于你在{mailing1}的家的事;
$search=array(
“{first}”,
“{last}”,
“{mailing1}”,
);
$replace=数组(
$person->first,
$person->last,
$person->mailing 1,
);
echo str_replace($search,$replace,$theString);
您可以做的是将
$theString
的值存储在数据库中,以便快速更改字母内容…以下是一种更简单的方法。也许你需要更多的复杂性,但对于这个例子,你不需要
$thePerson = $_POST['people_id'];
$theString = '<p>Dear {first} {last},</p><p>I am writing regarding your home at {mailing1}</p>';
$sql = $conn_read->prepare("
SELECT p.first AS `{first}`, p.last AS `{last}`, a.mailing1 AS `{mailing1}`
FROM people p
LEFT JOIN address a ON a.people_id = p.id
WHERE p.id = :people_id
");
$sql->execute(array(':people_id' => $thePerson));
$results = $sql->fetch(PDO::FETCH_ASSOC);
$output = str_replace(array_keys($results), array_values($results), $theString);
$thePerson=$\u POST['people\u id'];
$theString='亲爱的{first}{last},我写信是关于你在{mailing1}的家;
$sql=$conn\u read->prepare(“
选择p.first作为`{first}`,p.last作为`{last}`,a.mailing1作为`{mailing1}`
来自人民党
a.people\u id=p.id上的左连接地址a
其中p.id=:people\u id
");
$sql->execute(数组(':people\u id'=>$thePerson));
$results=$sql->fetch(PDO::fetch_ASSOC);
$output=str_replace(数组_键($results)、数组_值($results)、$theString);
请注意,我完全避免使用宏表,只需将sql结果集的字段命名为需要替换的$theString
的值,并仅选择这些字段,而不是使用select*
选择所有字段。另外,由于它只是一个字符串替换,不需要正则表达式,我将preg\u replace
切换为更简单、更快的str\u replace
在某些情况下,例如如果有不相关的表,则可能需要使用两个查询。因此,您只需执行两次
str\u replace
(如果执行此操作,请确保使用$output
作为第三个参数)。很好!我想我可以做一些甜选择,但可以概念化的语法。如果您需要任何信息,请随时联系我。mike在myk12.org。@Johannes提出了一个很好的观点,如果您需要制作许多不同的邮件模板/查询,您可以将$theString
及其查询存储在一个表中。