0

I have my controller :: controller.php
which has a call to my model but I already know what id's I want to search for so what I want to know is how would I send that array to my model so that my model searches those and returns the result

example:

controller.php

foreach($value as $val){
    $array[] = $val;
}
$search = $this->search_model->send_array($array);

search_model.php

public function send_array($array){
    $this->db->where($array[index]);
    //something like this
    return $this->db->get($this->table);
}

the array that is being sent is the primary key of the table so I don't know how to get back that data.

in the database example my array is the value of the ID and I want to get back those rows in only one return within my model

if in the array there is (1,3,4) i should get back John, Terry, and Anna

return $this->db->get($this->table);

Database table:

ID    NAME
1     John
2     Doe
3     Terry
4     Anna
learningbyexample
  • 1,527
  • 2
  • 21
  • 42
  • 1
    Use the `in()` sql operator http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in – Shadow Oct 18 '16 at 11:12
  • What framework are you using? Cause Core PHP doesnt have a DB layer like the one that you have mentioned in question. So you can tag the framework that youa re using. Or if it is custom built then we will be needing how the layer creates Queries. – Mohd Abdul Mujib Oct 18 '16 at 11:14
  • Possible duplicate of [PHP/MySQL using an array in WHERE clause](http://stackoverflow.com/questions/907806/php-mysql-using-an-array-in-where-clause) – Shadow Oct 18 '16 at 11:14
  • Use the answer by Levi Morrison (http://stackoverflow.com/a/23641033/5389997) instead of the accepted nswer. – Shadow Oct 18 '16 at 11:15

1 Answers1

1

You can use where_in like this:

public function send_array($array){
  $query = $this->db->where_in('id', $array);
  return $query->results();
}

More info about that in here http://www.codeigniter.com/user_guide/database/query_builder.html

vher2
  • 980
  • 6
  • 9