2015-12-10 145 views
3

我寫了一個代碼發送電子郵件到控制器中的多個電子郵件地址,但我想我寫錯了,我不確定我是否在forloop的寫入部分寫了明確的電子郵件。對不起,這可能是一個簡單的問題,但我是這個領域的一些新東西。Forloop錯誤codeigniter

public function sendEmailforUnpaidInvoiceFromDB() { 
    //fetch tha data from the database 
    $this->load->model('invoice_page'); 
    $foo = $this->invoice_page->getInvoicesForNotification(); 
    $result = json_decode(json_encode($foo[0]), true); 
    foreach ($foo as $result){ 
    $this->load->library('email'); 
    $this->email->from('[email protected]', 'HELLO'); 
    $this->email->to($result['Email']); 
    $this->email->subject('Pay Payments'); 
    //$msg = $this->load->view('mypayment_view', '', true); 
    $this->email->message('your due date is '.$result['duedate']); 
    $returnvalue = $this->email->send(); 


    if(!$returnvalue) { 
     alert("email failed to send"); 
    } else { 
    // $uptArray = array('customer_notified_dt' => NOW()); 
     //$this -> db -> update('invoice', $uptArray); 
     } 
    $this->email->clear(TRUE); 
    } 
} 

我從模型中獲得2個數組,包括Email和duedate。 錯誤圖片error using the url directlyerror using the button I placed

+0

爲什麼你認爲它的工作原理錯了嗎?你有沒有收到任何錯誤信息?如果是,請在這裏發佈。 –

+1

@ArthurGevorkyan我添加了錯誤圖片 – FaF

+0

現在你有更好的機會找到你的問題的幫助!祝你好運,男人。 –

回答

2

問題是在這裏,

$result = json_decode(json_encode($foo[0]), true); 
foreach ($foo as $result){ 
       ^// overwriting previous variable 

如果$result爲對象,請訪問使用->

$result->email; 

如果其陣陣Ÿ

$result['email']; 

編輯

你得到只有一個原因,

$result = json_decode(json_encode($foo[0]), true); 
            ^

你只考慮第一個索引列,刪除0爲指標。

+0

那麼你寫什麼來修復錯誤? – FaF

+0

將變量重命名爲'$ result1'。 –

+0

我沒有重命名變量,但如果我使用$ result-> email,它只適用於來自數據庫的第一行,不適用於第二行,並附有新的電子郵件地址信息 – FaF

2

您正試圖將對象讀取爲數組,如錯誤所述。嘗試更換這些:

​​

有了這些:

$this->email->to($result->Email); 
$this->email->message('your due date is '.$result->duedate); 
+0

非常感謝!有用! – FaF

+0

但是,當我有多行來自數據庫時它不起作用,只適用於第一個行@J_vdv – FaF

+0

執行$ result的print_r並查看其中的內容。然後以錯誤的方式提取數據。 –

0

試試這個:

public function sendEmailforUnpaidInvoiceFromDB() { 
     //fetch tha data from the database 
     $this->load->model('invoice_page'); 
     $foo = $this->invoice_page->getInvoicesForNotification(); 

     foreach ($foo as $result){ 
     $this->load->library('email'); 
     $this->email->from('[email protected]', 'HELLO'); 
     $this->email->to($result->Email); 
     $this->email->subject('Pay Payments'); 
     //$msg = $this->load->view('mypayment_view', '', true); 
     $this->email->message('your due date is '.$result->duedate); 
     $returnvalue = $this->email->send(); 


     if(!$returnvalue) { 
      alert("email failed to send"); 
     } else { 
     // $uptArray = array('customer_notified_dt' => NOW()); 
      //$this -> db -> update('invoice', $uptArray); 
      } 
     $this->email->clear(TRUE); 
     } 
    }