dryift6733 2016-10-18 11:09
浏览 55
已采纳

如何使用codeigniter框架从主键数组中获取查询

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
  • 写回答

1条回答 默认 最新

  • duanrang2627 2016-10-18 11:27
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊