Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forloop error codeigniter

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

like image 538
XAF Avatar asked Jul 03 '26 13:07

XAF


1 Answers

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 that 0 as index.

like image 113
Niranjan N Raju Avatar answered Jul 06 '26 02:07

Niranjan N Raju



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!