简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM