php
  • arrays
  • 2017-04-30 107 views 2 likes 
    2

    我有一個php循環,用於查詢數據庫併發送電子郵件給羣組。在這個例子中,它返回兩行。PHP array保留循環中的舊值

    問題是它包含第二次運行中第一次運行的結果。

    我的代碼:

    $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);     
        } 
    } 
    

    結果從數據庫:

    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 
    

    會發生什麼:

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

    我希望發生的

    爲什麼保留在第二次運行以前的值?

    +0

    你的數組名是什麼?我的意思是你打印哪個數組? – manian

    +0

    @manian print_r($ filtered_email); – osakagreg

    +0

    對不起,我找到了答案併發布了一個答案 – manian

    回答

    1

    試試這個,

    $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);     
        } 
    } 
    

    要回答你的問題,在這方面。您需要在使用這些變量之前重置循環中的變量。

    +0

    這肯定有所幫助,但第二次運行仍然重複舊值。只是以不同的方式。我在上面的原始問題中發佈了一個具體輸出的例子。 – osakagreg

    +0

    我想我現在明白了。只需在循環內而不是循環上面定義數組。我更新了答案。請檢查是否有幫助 – manian

    +0

    這也沒有得到它。我更新了原始問題的結果。 – osakagreg

    0

    您的問題是,您正在設置每個人的「電子郵件」變量中的值,但是您沒有將它們每次都設置爲空白。

    但是,您不應該有條件地設置這些變量,您應該有條件地加載您的email_arrayphone_array陣列,具體取決於它們的「名稱」列是否已填充到數據庫中。

    像這樣的事情會是乾淨了一點:

    $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(); 
        } 
    } 
    

    通知你不需要過濾陣列,因爲他們總是包含正確的信息。

    相關問題