0

Question

Basically I want to send reminder emails to the people who haven't accepted the challenge yet. From challenges table I get the emails who have been challenged and then from a_challenges table I get the emails (usernames) who have accepted the challenge.

Problem

I want to basically remove the email addresses which are in the a_challenges table from the array coming from challenges table as those people have already accepted the challenge so i don't want to send them an reminder email. Any help will be appreciated.

Code

<?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..
?>

Actual Output

Team==> AGS Larger Lads

Accepted==> Donald -- [email protected]

Accepted==> David -- [email protected]

Accepted==> Sean -- [email protected]

Send email to below mentioned address:

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

Expected Output

Team==> AGS Larger Lads

Accepted==> Donald -- [email protected]

Accepted==> David -- [email protected]

Accepted==> Sean -- [email protected]

Send email to below mentioned address:

[email protected]
[email protected]
[email protected]
[email protected]
7
  • 4
    what's the difference between the expected and the actual output? Commented May 28, 2013 at 11:56
  • 5
    SELECT ... FROM ... WHERE address NOT IN (SELECT address FROM teams) ...or something along those lines. Let the database do it! Commented May 28, 2013 at 11:57
  • 2
    Instead of doing this all in PHP you can do that in your mysql query as well. and i think this will be easy . Commented May 28, 2013 at 11:57
  • Why don't you just select those addresses only, to which a reminder message wasn't sent? Commented May 28, 2013 at 11:59
  • 1
    @colourtheweb And that is why you normalize your database and do not store several values in the same field! :P Commented May 28, 2013 at 12:01

3 Answers 3

3

You don't want to solve this in PHP, you want to restrict these results in the query from the database. Essentially the code should be this:

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

This is of course speculation until you can post the full schema.

Sign up to request clarification or add additional context in comments.

Comments

2

I think the easiest would be, to not remove the email address from the string, before you explode it it into the array (btw terrible way of storing email addresses in databases)

anyway, i just assumed that $row_teams['username'] is the email address here is i think the most simplest way to do it with your code:

    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..

Comments

1

I assume, the username field contains the E-Mail, so you could simply

//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

1 Comment

It's working for me but am getting this error "Notice: Undefined offset: 1 in /var/www/vhosts/disciplinexgames.com/httpdocs/dx_scores/emails.php on line 31 "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.