【问题标题】:Forloop error codeigniterForloop错误代码点火器
【发布时间】:2016-03-15 20:05:00
【问题描述】:

我已经编写了一个代码,用于在控制器中向多个电子邮件地址发送电子邮件,但我想我写错了,我不确定我是否也在 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 个数组,包括电子邮件和到期日期。 错误图片

【问题讨论】:

  • 你认为它为什么会出错?您收到任何错误消息吗?如果是,请在此处发布。
  • @ArthurGevorkyan 我添加了错误图片
  • 现在您有更好的机会就您的问题寻求帮助!祝你好运,伙计。

标签: php codeigniter for-loop controller


【解决方案1】:

问题来了,

$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 作为索引删除。

【讨论】:

  • 将变量重命名为$result1
  • 我确实重命名了变量,但如果我使用 $result->email 它仅适用于来自数据库的第一行,不适用于具有新电子邮件地址信息的第二行
  • 您在哪里给我们发电子邮件?在 foo 或 $result??
  • Foo 获取 email 和 duedate ,结果将其更改为 json decode,因此使用 $result 可以工作
  • 错误在这里$result = json_decode(json_encode($foo[0]), true); 你只接受$foo[0]。所以它只工作一次。
【解决方案2】:

试试这个:

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 但是当我有来自数据库的多行时它不起作用,只适用于第一行@J_vdv
    • 做一个 $result 的 print_r 看看里面有什么。然后以错误的方式获取数据。
    • 就像 Niranjan N Raju 所说,在你的 json_decode 中将 $foo[0] 更改为 $foo。 $foo[0] 只获取对象的第一个索引。
    • 是的,我刚刚修好了,谢谢!我不确定我是否可以对你和 Niranjan 的答案都打勾,因为他们都有帮助。
    • 你不能,但没关系。 Niranjan 是第一个提出这个建议的人。我只是把你引向了正确的方向。请记住始终使用 print_r($variable);对于这种东西。然后你就可以看到发生了什么。
    猜你喜欢
    • 2011-09-27
    • 2016-01-13
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    相关资源
    最近更新 更多