问题
基本上我想向尚未接受挑战的人发送提醒电子邮件。在挑战表格中,我会收到受到质疑的电子邮件,然后从 a_challenges 表中收到已接受质询的电子邮件(用户名)。
问题
我想基本上从挑战表中删除 a_challenges 表格中的电子邮件地址,因为那些人已经接受了挑战所以我不知道我想给他们发一封提醒邮件。任何帮助将不胜感激。
代码
<?php
require_once('includes/dbConnector.php');
$dbCon = new dbConnector();//define the DB connection
$query_challenges = "Select distinct * from challenges";//query to fetch records from challenges table like usernames who have been challenged
//echo $query_challenges;
$result_challenges = $dbCon->query($query_challenges);//execute query
//start while loop to fetch the records
while($row_tr = $dbCon->fetchArray($result_challenges)){
$t_name = $row_tr['t_name'];
$c_emails = $row_tr['c_emails'];
echo "<br /><br />".$t_name."<br /><br />";
$query_a_challenges = "Select * from a_challenges where t_name='".$t_name."'";//query to fetch records from a_challenges table like usernames who have accepted the challenge, etc
//echo $query_a_challenges;
$result_a_challenges = $dbCon->query($query_a_challenges);//execute query
while($row_a_challenges = $dbCon->fetchArray($result_a_challenges)){
echo "Accepted==> ". $row_a_challenges['full_name']." -- ";
echo $row_a_challenges['username']."<br /><br />";
}//end while a_challenges..
//echo $c_emails."<br /><br />";
$c_emails = explode(',', $c_emails);
for($i=0; $i<count($c_emails); $i++){
echo $c_emails[$i]."<br />";
}//end for
}//end while challenges..
?>
实际输出
Team==> AGS Larger Lads
Accepted==> Donald -- donald@example.com
Accepted==> David -- david@example.com
Accepted==> Sean -- Sean@example.com
发送电子邮件至以下地址:
byrne@example.com
neil@example.com
brendan@example.com
Sean@example.com
donald@example.com
saunders@example.com
david@example.com
预期输出
Team==> AGS Larger Lads
Accepted==> Donald -- donald@example.com
Accepted==> David -- david@example.com
Accepted==> Sean -- Sean@example.com
发送电子邮件至以下地址:
byrne@example.com
neil@example.com
brendan@example.com
saunders@example.com
答案 0 :(得分:3)
您不希望在PHP中解决此问题,您希望在数据库的查询中限制这些结果。基本上代码应该是这样的:
select request.*, team.*
from teams team, team_request request
where team.name=request.team_name
and request.accepted = false
这当然是推测,直到你可以发布完整的架构。
答案 1 :(得分:2)
我认为最简单的方法是,在将它分解为数组之前不要从字符串中删除电子邮件地址(顺便说一句,将数据库中的电子邮件地址存储在数据库中)
无论如何,我只是假设$ row_teams ['username']是电子邮件地址 这是我认为用你的代码做最简单的方法:
while($row_tr = $dbCon->fetchArray($result_team_request)){
$t_name = $row_tr['t_name'];
$c_emails = $row_tr['c_emails'];
echo "<br /><br />".$t_name."<br /><br />";
$query_teams = "Select * from teams where t_name='".$t_name."'";//query to fetch records from teams table like usernames who have accepted the challenge, etc
//echo $query_teams;
$result_teams = $dbCon->query($query_teams);//execute query
while($row_teams = $dbCon->fetchArray($result_teams)){
echo "Accepted==> ". $row_teams['full_name']." -- ";
echo $row_teams['username']."<br /><br />";
//my new code line:
$c_emails = str_replace($row_teams['username'],'',$c_emails);
}//end while teams..
$c_emails = explode(',', $c_emails);
//echo $c_emails."<br /><br />";
for($i=0; $i<count($c_emails); $i++){
echo $c_emails[$i]."<br />";
}//end for
}//end while team_request..
答案 2 :(得分:1)
我认为,username
字段包含电子邮件,因此您可以简单地
//start while loop to fetch the records
//*** NEW LINE HERE ***
$usedmails=array();
while($row_tr = $dbCon->fetchArray($result_team_request)){
$t_name = $row_tr['t_name'];
$c_emails = $row_tr['c_emails'];
echo "<br /><br />".$t_name."<br /><br />";
$query_teams = "Select * from teams where t_name='".$t_name."'";//query to fetch records from teams table like usernames who have accepted the challenge, etc
//echo $query_teams;
$result_teams = $dbCon->query($query_teams);//execute query
while($row_teams = $dbCon->fetchArray($result_teams)){
echo "Accepted==> ". $row_teams['full_name']." -- ";
echo $row_teams['username']."<br /><br />";
//*** NEW LINE HERE ***
$usedmails[]=$row_teams['username'];
}//end while teams..
//echo $c_emails."<br /><br />";
$c_emails = explode(',', $c_emails);
// *** NEW LINE HERE ***
$c_emails=array_diff($c_emails,$usedmails);
for($i=0; $i<count($c_emails); $i++){
echo $c_emails[$i]."<br />";
}//end for