php - Forloop error codeigniter

950

I have written a code for sending emails to multiple emailaddress in the controller, but I guess I have written it wrong, and I am not sure if I wrote the clear email in the write part of the forloop as well. Sorry this might be a easy question but I am some what new in this field.

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

I am getting 2 arrays from the model, including Email and duedate. Error picture error using the url directly, and error using the button I placed

19

Answer

Solution:

Problem is here,

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

If$result is object, access using->

 $result->email;

If its an array

 $result['email'];

EDIT

you are getting only one row because,

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

you are considering only first index., remove that0 as index.

838

Answer

Solution:

You are trying to read objects as arrays, like the error says. Try to replace these:

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

With these:

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

Answer

Solution:

Try this One:

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

People are also looking for solutions to the problem: ubuntu - iptables block outgoing request from php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.