当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How do I get a query from an array of primary keys using codeigniter framework

问题描述:

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

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: