2

I have a php loop that is meant to query a database and send out email to groups of people. In this example, it returns two rows.

The problem is that it includes the results of the first run in the 2nd run.

My Code:

 $query = mysql_query("SELECT * FROM tasks WHERE DATE(`show_date`) = (      CURDATE() - INTERVAL 1 DAY ) AND drive_folder_empty = 'empty' AND drive_folder IS NOT NULL;");
    while ($row = mysql_fetch_assoc($query)){

        if (!empty($row['robert'])){$robert_email = "robert email"; $robert_phone = "robert phone";}
        if (!empty($row['duncan'])){$duncan_email = "duncan email"; $duncan_phone = "duncan phone";}
        if (!empty($row['mike'])){$mike_email = "mike email"; $mike_phone = "mike phone";}
        if (!empty($row['james'])){$james_email = "james email"; $james_phone = "james phone";}


        $email_array = array($robert_email, $duncan_email, $mike_email, $james_email);              
        $filtered_email = array_filter($email_array);

        print_r($filtered_email);

        $mail_to = implode(', ', $filtered_email);

        $phone_array = array($robert_phone, $duncan_phone, $mike_phone, $james_phone);              
        $filtered_phone = array_filter($phone_array);           
        $cc = implode(', ', $filtered_phone);           
        if (!empty($mail_to))
            sendEmail($mail_to, $drive_url, $date_of_show, $cc);                
    }
}

Results From Database:

ID  EID     drive_folder    drive_folder_name   drive_folder_empty  duncan  robert  mike    james   partners_name   completed_form  all_forms_in    priority    ask_for_review  blog_status     bloggers_email  todays_date     show_date
20  2457    drive url       drive name          empty               Duncan  Robert  NULL    NULL    NULL            n               n               NULL        NULL            NULL            NULL            2017-04-24      2017-04-29 
21  2468    drive url       drive name          empty               NULL    NULL    Mike    James   NULL            n               n               NULL        NULL            NULL            NULL            2017-04-24      2017-04-29

What Happens:

Array
(
  [0] => robert email
  [1] => duncan email
)
Array
(
  [0] => robert email
  [1] => duncan email
  [2] => mike email
  [3] => james email
)

What I Want To Happen

Array
(
  [0] => robert email
  [1] => duncan email
)
Array
(
  [0] => mike email
  [1] => james email
)

Why is it retaining the previous values on the 2nd run?

3
  • What is your array name? I meant which array you printed? Commented Apr 30, 2017 at 19:37
  • @manian print_r($filtered_email); Commented Apr 30, 2017 at 19:39
  • I am sorry, I got it & posted an answer Commented Apr 30, 2017 at 19:42

2 Answers 2

1

Try this,

  $query = mysql_query("SELECT * FROM tasks WHERE DATE(`show_date`) = (      CURDATE() - INTERVAL 1 DAY ) AND drive_folder_empty = 'empty' AND drive_folder IS NOT NULL;");
  while ($row = mysql_fetch_assoc($query)){
     $robert_email = $duncan_email = $mike_email = $james_email = $robert_phone = $duncan_phone = $mike_phone = $james_phone = '';
    if (!empty($row['robert'])){$robert_email = "robert email"; $robert_phone = "robert phone";}
    if (!empty($row['duncan'])){$duncan_email = "duncan email"; $duncan_phone = "duncan phone";}
    if (!empty($row['mike'])){$mike_email = "mike email"; $mike_phone = "mike phone";}
    if (!empty($row['james'])){$james_email = "james email"; $james_phone = "james phone";}


    $email_array = array($robert_email, $duncan_email, $mike_email, $james_email);              
    $filtered_email = array_filter($email_array);

    print_r($filtered_email);

    $mail_to = implode(', ', $filtered_email);

    $phone_array = array($robert_phone, $duncan_phone, $mike_phone, $james_phone);              
    $filtered_phone = array_filter($phone_array);           
    $cc = implode(', ', $filtered_phone);           
    if (!empty($mail_to))
        sendEmail($mail_to, $drive_url, $date_of_show, $cc);                
   }
}

To answer your question, in this context. you need to reset your variables in your loop before using those.

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

6 Comments

That certainly helps a bit, but the second run, is still repeating old values. Just in a different manner. I posted an example of that specific output, above in the original question.
I think I got it now. Just define the array inside the loop instead of above the loop. I updated the answer. Please check if this helps
That didn't get it either. I updated the results in the original question.
Didn't read your question well. Just updated my answer. Can you try now.
THANK YOU! I guess I just needed to reset those values? Anyhow, that worked exactly how I wanted. I appreciate your help!
|
0

Your problem is that you are setting the value in each person's 'email' variable but you aren't setting them back to blank each time.

However, you shouldn't conditionally set those variables, you should conditionally load up your email_array and phone_array arrays based on whether or not their 'name' column was populated in the database.

Something like this would be a little cleaner:

$people = Array(
    'robert' => Array(
        'email' => 'robert email',
        'phone' => 'robert phone'
    ),
    'duncan' => Array(
        'email' => 'duncan email',
        'phone' => 'duncan phone'
    ),
    'mike' => Array(
        'email' => 'mike email',
        'phone' => 'mike phone'
    ),
    'james' => Array(
        'email' => 'james email',
        'phone' => 'james phone'
    )
);



while ($row = mysql_fetch_assoc($query)) {

    // loop through each person and load up
    // the arrays if the name col is not empty
    foreach ($people as $name => $contact_info) {
        if ($row[$name]) {
            $email_array[] = $contact_info['email'];
            $phone_array[] = $contact_info['phone'];
        }
    }

    if (count($email_array) > 0) {
        $mail_to = implode(', ', $email_array);
        $cc = implode(', ', $phone_array);

        sendEmail($mail_to, $drive_url, $date_of_show, $cc);

        $email_array = Array();
        $phone_array = Array();
    }
}

Notice you don't need to filter the arrays, as they always contain the correct info.

Comments

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.