osakagreg
osakagreg

Reputation: 577

PHP array retains old values in loop

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?

Upvotes: 2

Views: 247

Answers (2)

BizzyBob
BizzyBob

Reputation: 14740

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.

Upvotes: 0

manian
manian

Reputation: 1438

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.

Upvotes: 1

Related Questions