gpt4 book ai didi

php - 如何让 php 在循环外回显值?

转载 作者:太空宇宙 更新时间:2023-11-03 12:03:11 25 4
gpt4 key购买 nike

我希望能够将此人的姓名放入电子邮件中。但是我不知道如何在循环外保留值。另外,我想知道如果结果不止一个,这是否可行。

这是 php:

// WHO HAS BEEN A MEMBER FOR ONE YEAR
function joinDateFilter(){
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
$mail_to = "";
while ($row = mysql_fetch_array($query)){
echo $row['name']." - ".$row['email']."\n";
$mail_to = $row['email'].", ";
$name = $row['name'];
}
if (!empty($mail_to)){
sendEmail($mail_to);
}
}

// SEND EMAIL
function sendEmail($mail_to) {
$from = "webmaster@mydomain.com";
$message = "Happy Anniversary!";
$headers = 'From: '.$from."\r\n" .
'Reply-To:'.$_POST['email']."\r\n" .
"Content-Type: text/html; charset=iso-8859-1\n".
'X-Mailer: PHP/' . phpversion();
mail($mail_to, "Congratulations ".$name."!", $message, $headers);
}

在上面的例子中,邮件从不读取他们的名字,因为这个值似乎在 while 循环之外被遗忘了。有人可以帮助我吗?

最佳答案

目前,您的代码会在 while 循环的每次迭代中覆盖存储在 $mail_to 中的电子邮件 ID。因此,它将只有最后一个用户的电子邮件 ID。并且您需要每个用户的名称(存储在 $name 中并在函数中使用)。只要找到用户,您就需要发送电子邮件。试试这个:

// WHO HAS BEEN A MEMBER FOR ONE YEAR
function joinDateFilter(){
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
while ($row = mysql_fetch_array($query)){
echo $row['name']." - ".$row['email']."\n";
$mail_to = $row['email'];
$name = $row['name'];
if (!empty($mail_to))
sendEmail($mail_to, $name);
}
}

// SEND EMAIL
function sendEmail($mail_to, $name) {
$from = "webmaster@mydomain.com";
$message = "Happy Anniversary!";
$headers = 'From: '.$from."\r\n" .
'Reply-To:'.$_POST['email']."\r\n" .
"Content-Type: text/html; charset=iso-8859-1\n".
'X-Mailer: PHP/' . phpversion();
mail($mail_to, "Congratulations ".$name."!", $message, $headers);
}

关于php - 如何让 php 在循环外回显值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27994834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com