将数组从控制器传递到codeigniter中的模型

时间:2012-12-27 05:01:09

标签: php arrays codeigniter

我在Codeigniter做一个项目。在这里,我使用imap从我的gmail id中获取最新的10封邮件。我想从获取邮件的from字段获取,我想检查已获取邮件的from字段是否在我的数据库table('clients')中。在这里,我将10个从已获取邮件的字段存储到数组,并将其传递给模型,其中检查正在进行并返回匹配的字段名称。但它不适合我。

我的控制器功能是:

if ($mbox = imap_open($authhost, $user, $pass)) {
                $emails = imap_search($mbox, 'ALL');
                $some = imap_search($mbox, 'SUBJECT "Suspicious sign in prevented"', SE_UID);
                $MC = imap_check($mbox);
                $inbox = $MC->Nmsgs;
                $inboxs = $inbox - 9;
                if ($emails) {
                    $data['overview'] = imap_fetch_overview($mbox, "$inboxs,$inboxs:$inbox", 0);
                    $i = 0;
                    foreach ($data['overview'] as $i => $val) {

                        $from[$i] = $val->from;
                        $i++;
                    }

                    $data['result'] = $this->crm_model->get_names($from);
                    foreach ($data['result'] as $row) {
                        echo $row->name;echo "<br/>";
                    }

                }
                imap_close($mbox);
            }

我的模特功能是:

function get_names($from) {

    $this->db->select('name');
    $this->db->from('clients');
    $this->db->where_in('name', $from);
    $query = $this->db->get();
    return $query->result();
}

但是当我使用上面的模型函数时,它会返回值

function get_names() {
                  $options = array(
                         '0' => 'Job Openings',
                         '1' => 'Offers',
                         '2' => 'Techgig',
                         '3' => 'Australia',
                        );

        $this->db->select('name');
        $this->db->from('clients');
        $this->db->where_in('name', $options);
        $query = $this->db->get();
        return $query->result();
    }

我认为问题在于将值从控制器传递到模型。谁能帮我。提前致谢

2 个答案:

答案 0 :(得分:2)

使用where_in(逗号分隔值列表)在数据库中执行任何查询时, 值列表应该像这个数组:

$options = array('Hello', 'World!', 'Beautiful', 'Day!');

不喜欢这样:

 $options = array('0' => 'Job Openings','1' => 'Offers','2' => 'Techgig','3' =>'Australia',);

这样做你应该破坏这个阵列!

$opt=implode(",",$options);

并将此$ opt数组传递给WHERE_IN()

希望这能解决您的问题!

答案 1 :(得分:1)

您不必使用$i ..

if ($emails) {
                $data['overview'] = imap_fetch_overview($mbox, "$inboxs,$inboxs:$inbox", 0);
               // $i = 0;
                foreach ($data['overview'] as $val) {

                    $from[] = $val->from;
                    //$i++;
                }

                $data['result'] = $this->crm_model->get_names($from);
                foreach ($data['result'] as $row) {
                    echo $row->name;echo "<br/>";
                }

            }

进行这些更改并检查

相关问题