Forloop错误codeigniter

时间:2015-12-10 09:07:07

标签: php codeigniter for-loop controller

我已经编写了一个代码,用于向控制器中的多个电子邮件地址发送电子邮件,但我想我写错了,而且我不确定我是否也在forloop的写入部分编写了明确的电子邮件。对不起,这可能是一个简单的问题,但我是这个领域的新人。

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('helloworld@hello.com', '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

3 个答案:

答案 0 :(得分:2)

您正在尝试将对象作为数组读取,就像错误所说的那样。尝试替换这些:

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

有了这些:

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

答案 1 :(得分: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删除为索引。

答案 2 :(得分: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('helloworld@hello.com', '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);   
        }
    }