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

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

报告相同问题?

悬赏问题

  • ¥20 c语言写的8051单片机存储器mt29的模块程序
  • ¥60 求直线方程 使平面上n个点在直线同侧并且距离总和最小
  • ¥50 java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。
  • ¥50 rk3588板端推理
  • ¥250 opencv怎么去掉 数字0中间的斜杠。
  • ¥15 这种情况的伯德图和奈奎斯特曲线怎么分析?
  • ¥250 paddleocr带斜线的0很容易识别成9
  • ¥15 电子档案元素采集(tiff及PDF扫描图片)
  • ¥15 flink-sql-connector-rabbitmq使用
  • ¥15 zynq7015,PCIE读写延时偏大