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 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口