PHP - 在 foreach 语句之外重复脚本
PHP - Script repeating when outside foreach statement
我编写的脚本有问题,它只是从数据库中选择条目并向用户发送电子邮件以更新与他们相关的条目。
emailer 功能正在工作,条目过滤到各个用户。但是,我遇到了一个问题,即发送电子邮件的次数与条目在数据库中出现的次数相同。该电子邮件包含相同的内容(所有 4 个条目都出现在同一封电子邮件中,但以发送 4 次为例)。
这是邮件脚本:
$data = getData();
$uniqueUsers = array();
foreach($data as $item){
$user = $item[1];
$name = $item[3];
$array = array($user, $name);
if(!in_array($array, $uniqueUsers)){
array_push($uniqueUsers, $array);
};
};
echo print_r($uniqueUsers);
echo "<br>";
echo "<hr>";
foreach($uniqueUsers as $user){
$email = $user[0];
$name = $user[1];
$fullName = explode(" ", $name);
$firstName = $fullName[0];
$updates = "";
echo $email;
echo "<br>";
echo $name;
echo "<br>";
foreach($data as $item){
if($user[0] == $item[1]){
$equipmentName = $item[0];
$approved = $item[2];
$start = $item[5];
$end = $item[6];
$updates .= $approved . " - " . $equipmentName . " (Booked from " . date('l, jS F', strtotime($start)) . " to " . date('l, jS F', strtotime($end)) . ")";
$updates .= "<br>";
};
};
echo $updates;
echo "<hr>";
mailer($email, $firstName, $updates);
};
这是 getdata() 脚本:
mysql_select_db("WBLResources", $con) or die ("Error selecting specified database on mysql server: ".mysql_error());
$query = "SELECT * FROM `Bookings` WHERE `updated` > '" . $date . "'";
$result = mysql_query($query);
$numOfRows = mysql_num_rows($result);
if($numOfRows < 1){
exit();
};
for ($i = 0; $i < $numOfRows; $i++) {
$row = mysql_fetch_array($result);
$result_array[$i] = array($row["equipment_name"],
$row["user"],
$row["approved"],
$row["name"],
$row["updated"],
$row["start"],
$row["end"]);
};
return $result_array;
};
根据我的分析,mailer() 函数应该 运行 在 uniqueUsers 数组上。这是我 10 分钟前提取的片段:
Array (
[0] => Array (
[0] =>
[1] => )
[1] => Array (
[0] => ####@###.co.uk
[1] => Username )
[2] => Array (
[0] => ####@###.co.uk
[1] => Username )
[3] => Array (
[0] => #####@###.co.uk
[1] => Username )
[4] => Array (
[0] => #####@###.co.uk
[1] => Username )
[5] => Array (
[0] => ######@###.co.uk
[1] => Username )
[6] => Array (
[0] => #####@###.co.uk
[1] => Username ) )
邮件程序函数:`function mailer($email, $firstName, $updates){
$mail = new PHPMailer;
$subject = "Resource Bookings";
$htmlMessageBody = "<div style=font-size:10.0pt;font-family:'Arial',sans-serif;color:black;mso-fareast-language:EN-GB'>Dear " . $firstName . ",<br><br>The following resource bookings have been updated: <br><br>" . $updates . "<br><br>Many thanks,<br>TSW Support</div>";
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '#######'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '###@####.co.uk'; // SMTP username
$mail->Password = '######';
$mail->SMTPSecure = 'tls';
$mail->Port = 25; // TCP port to connect to
//$mail->SMTPDebug = 3;
$mail->setFrom('####@#####.co.uk', 'TSW Training Group');
$mail->addAddress($email, $firstName); // Add a recipient
$mail->addReplyTo('#####@######.co.uk', 'TSW Training Group');
$mail->addCC("#####@#####.co.uk", "Support");
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $htmlMessageBody;
//$mail->AltBody = $plainMessageBody;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
};
};`
任何帮助将不胜感激?
这个逻辑应该可以解决您的问题。虽然还没有测试过,所以如果您遇到任何错误,请告诉我。
$user_lists = array();
foreach($data as $item){
$user = $item[1];
$name = $item[3];
$array = array($user, $name);
if(!in_array($user, $user_lists)){
array_push($uniqueUsers, $array);
$user_lists[] = $user;
};
};
希望对您有所帮助!
我编写的脚本有问题,它只是从数据库中选择条目并向用户发送电子邮件以更新与他们相关的条目。 emailer 功能正在工作,条目过滤到各个用户。但是,我遇到了一个问题,即发送电子邮件的次数与条目在数据库中出现的次数相同。该电子邮件包含相同的内容(所有 4 个条目都出现在同一封电子邮件中,但以发送 4 次为例)。
这是邮件脚本:
$data = getData();
$uniqueUsers = array();
foreach($data as $item){
$user = $item[1];
$name = $item[3];
$array = array($user, $name);
if(!in_array($array, $uniqueUsers)){
array_push($uniqueUsers, $array);
};
};
echo print_r($uniqueUsers);
echo "<br>";
echo "<hr>";
foreach($uniqueUsers as $user){
$email = $user[0];
$name = $user[1];
$fullName = explode(" ", $name);
$firstName = $fullName[0];
$updates = "";
echo $email;
echo "<br>";
echo $name;
echo "<br>";
foreach($data as $item){
if($user[0] == $item[1]){
$equipmentName = $item[0];
$approved = $item[2];
$start = $item[5];
$end = $item[6];
$updates .= $approved . " - " . $equipmentName . " (Booked from " . date('l, jS F', strtotime($start)) . " to " . date('l, jS F', strtotime($end)) . ")";
$updates .= "<br>";
};
};
echo $updates;
echo "<hr>";
mailer($email, $firstName, $updates);
};
这是 getdata() 脚本:
mysql_select_db("WBLResources", $con) or die ("Error selecting specified database on mysql server: ".mysql_error());
$query = "SELECT * FROM `Bookings` WHERE `updated` > '" . $date . "'";
$result = mysql_query($query);
$numOfRows = mysql_num_rows($result);
if($numOfRows < 1){
exit();
};
for ($i = 0; $i < $numOfRows; $i++) {
$row = mysql_fetch_array($result);
$result_array[$i] = array($row["equipment_name"],
$row["user"],
$row["approved"],
$row["name"],
$row["updated"],
$row["start"],
$row["end"]);
};
return $result_array;
};
根据我的分析,mailer() 函数应该 运行 在 uniqueUsers 数组上。这是我 10 分钟前提取的片段:
Array (
[0] => Array (
[0] =>
[1] => )
[1] => Array (
[0] => ####@###.co.uk
[1] => Username )
[2] => Array (
[0] => ####@###.co.uk
[1] => Username )
[3] => Array (
[0] => #####@###.co.uk
[1] => Username )
[4] => Array (
[0] => #####@###.co.uk
[1] => Username )
[5] => Array (
[0] => ######@###.co.uk
[1] => Username )
[6] => Array (
[0] => #####@###.co.uk
[1] => Username ) )
邮件程序函数:`function mailer($email, $firstName, $updates){
$mail = new PHPMailer;
$subject = "Resource Bookings";
$htmlMessageBody = "<div style=font-size:10.0pt;font-family:'Arial',sans-serif;color:black;mso-fareast-language:EN-GB'>Dear " . $firstName . ",<br><br>The following resource bookings have been updated: <br><br>" . $updates . "<br><br>Many thanks,<br>TSW Support</div>";
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '#######'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '###@####.co.uk'; // SMTP username
$mail->Password = '######';
$mail->SMTPSecure = 'tls';
$mail->Port = 25; // TCP port to connect to
//$mail->SMTPDebug = 3;
$mail->setFrom('####@#####.co.uk', 'TSW Training Group');
$mail->addAddress($email, $firstName); // Add a recipient
$mail->addReplyTo('#####@######.co.uk', 'TSW Training Group');
$mail->addCC("#####@#####.co.uk", "Support");
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $htmlMessageBody;
//$mail->AltBody = $plainMessageBody;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
};
};`
任何帮助将不胜感激?
这个逻辑应该可以解决您的问题。虽然还没有测试过,所以如果您遇到任何错误,请告诉我。
$user_lists = array();
foreach($data as $item){
$user = $item[1];
$name = $item[3];
$array = array($user, $name);
if(!in_array($user, $user_lists)){
array_push($uniqueUsers, $array);
$user_lists[] = $user;
};
};
希望对您有所帮助!