Balu
Balu

Reputation: 607

Pass an array from controller to model in codeigniter

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.

My controller function is:

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

And my model function is:

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

Upvotes: 4

Views: 3376

Answers (2)

sandip
sandip

Reputation: 3289

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!');

Not like this:

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

to do this you should implode this array!

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

and pass this $opt array to WHERE_IN()

Hope this will solve your problem !

Upvotes: 2

Bhuvan Rikka
Bhuvan Rikka

Reputation: 2703

You don't have to use $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/>";
                }

            }

Do these changes and check

Upvotes: 1

Related Questions