将数组从控制器传递到Codeigniter中的模型 [英] Pass an array from controller to model in codeigniter

查看:83
本文介绍了将数组从控制器传递到Codeigniter中的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am doing a project in Codeigniter. Here I fetch the latest 10 mails from my gmail id using imap.Here I want to take the from field of fetched mail and i want to check whether the from field of fetched mail is in my database table('clients'). Here I stored the 10 from field of fetched mail to array and passed it to model,where the checking is takingplace and returns the matching field name. But it is not working for me.

我的控制器功能是:

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

但是当我像下面那样使用上面的模型函数时,它返回值

But when I used the above model function like below, it returns the value

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

我认为问题在于将值从控制器传递给模型。谁能帮我。在此先感谢

I think the problem is with passing value from controller to model. Can anyone help me. Thanks in advance

推荐答案

在数据库中使用where_in(逗号分隔值列表)执行任何查询时,
会在列表中的值应类似于此数组:

While executing any query in database using where_in (list of comma separated values), the list of values should be like this array:

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

不是这样的:

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

要执行此操作,您应该内爆该数组!

to do this you should implode this array!

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

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

and pass this $opt array to WHERE_IN()

希望这可以解决您的问题!

Hope this will solve your problem !

这篇关于将数组从控制器传递到Codeigniter中的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆