0

问题

基本上我想向尚未接受挑战的人发送提醒电子邮件。从挑战表中我得到被挑战的电子邮件,然后从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
4

3 回答 3

3

您不想在 PHP 中解决这个问题,您想将这些结果限制在来自数据库的查询中。基本上代码应该是这样的:

select request.*, team.*  
from teams team, team_request request  
where team.name=request.team_name  
and request.accepted = false

这当然是猜测,直到您可以发布完整的架构。

于 2013-05-28T11:59:05.273 回答
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..
于 2013-05-28T12:06:46.313 回答
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
于 2013-05-28T12:02:34.123 回答