我有我的 Controller ::controller.php
它调用了我的模型,但我已经知道我想要搜索什么 id,所以我想知道如何将该数组发送到我的模型,以便我的模型搜索这些并返回结果
示例:
Controller .php
foreach($value as $val){
$array[] = $val;
}
$search = $this->search_model->send_array($array);
搜索模型.php
public function send_array($array){
$this->db->where($array[index]);
//something like this
return $this->db->get($this->table);
}
正在发送的数组是表的主键,所以我不知道如何取回该数据。
在数据库示例中,我的数组是 ID 的值,我想在模型中仅通过一次返回来取回这些行
如果数组中有 (1,3,4),我应该返回 John、Terry 和 Anna
return $this->db->get($this->table);
数据库表:
ID NAME
1 John
2 Doe
3 Terry
4 Anna
最佳答案
您可以像这样使用where_in
:
public function send_array($array){
$query = $this->db->where_in('id', $array);
return $query->results();
}
有关此内容的更多信息,请参见此处 http://www.codeigniter.com/user_guide/database/query_builder.html
关于php - 如何使用 codeigniter 框架从主键数组中获取查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40106627/