4

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

5
  • 1
    try by printing $query->result_array() in model itself! Commented Dec 27, 2012 at 5:19
  • @sandip I tested $query->result_array() and echo the result there.not returing any value an empty page only Commented Dec 27, 2012 at 6:03
  • wait a minute you echoed it & it displayed empty array() right? Commented Dec 27, 2012 at 6:06
  • try by print_r() it prints array Commented Dec 27, 2012 at 6:09
  • @sandip I tried foreach ($query->result_array() as $row) { echo $row['name']; } in model it returns nothing.an empty page. Commented Dec 27, 2012 at 6:11

2 Answers 2

2

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 !

Sign up to request clarification or add additional context in comments.

3 Comments

When i tried using imploded array , it will not return anything.empty screen only.
No.When I used the array $options = array('0' => 'Job Openings','1' => 'Offers','2' => 'Techgig','3' =>'Australia',); it returns the result.When i implode the same array like this Job Openings,Offers,Techgig,Australia.and pass the imploded array to where_in clause it will not return value.
implode() converts your array into a string.. You cannot pass string to where_in().. So this method fails
1

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

9 Comments

I have checked with your suggestion.But not returning any value.empty page only.
@Balu Okay now you are storing the $val->from in $from[] right.. print_r($from) after the foreach loop in your controller and check what you get
@Balu If everything is fine there, print_r($from) in your model function and check your data.. If that is also fine,then the empty page you are getting would be a database issue
I got an array like this Array ( [0] => "Anitha (HR)" [1] => Techgig [2] => Govt Jobs [3] => Techgig ....) when using print_r($from) in both controller and model.
But when i use an array like $options = array( '0' => 'Job Openings', '1' => 'Offers', '2' => 'Techgig'); it returns value.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.