简体   繁体   中英

Codeigniter db select where as an array

I have this code i need to use to get results from a database, I'm on codeigniter. This is how the query looks like,

        $this->db->select('*');
        $this->db->where('broadcast_id', $bid);
        $query = $this->db->get('ih_noticeboard');
        $result = $query->result();
        if($query->num_rows() < 1){
            $data = "no feed ";
        }else{
            $data = $result;
        }

the $bid in the where clause is got from this code,

    $this->db->select('broadcast_id');
    $this->db->where('broadcast_subscribers', $id);
    $query = $this->db->get('ih_broadcastors    ');
    if($query->num_rows() < 1){
        $data = "user not subscribed to any broadcasting";
    }else{
        $ress = $query->result();
        foreach ($ress as $row){
            $bid = $row->broadcast_id;
        }`

Now the select uses only one $bid, how can I use the $bid as an array?

It's quite simple . just use $this->db->where_in('broadcast_id', $bid);

instead if $this->db->where('broadcast_id', $bid);

https://ellislab.com/codeigniter/user-guide/database/active_record.html#select

Wouldn't it be better to have one query to get the feeds?

$this->db->select("ih_noticeboard.*");
$this->db->from("ih_noticeboard");
$this->db->join("ih_broadcastors", "ih_noticeboard.broadcast_id = ih_broadcasters.broadcast_id");
$this->db->where("ih_broadcastors.broadcast_subscribers", $id);
$result->db->get()->result();

Assuming you want to select in the table where

name='jane' and broadcast_id=2 #as an example

        $bid=array(
           'name'=>'jane',
           'broadcast_id'=>'2'
                  ) 

        $this->db->where($bid);
        $this->db->from($table_name);
        $query = $this->db->get()->result_array();
        return $query;

Please notice that the $bid as array must be formated to suite the next database table to be queried. You can do this using foreach loop Eg

   foreach ($ress as $row){
        $bid = array(
             'name'=>$row['name'],
             'broadcast_id'=>$row['id']
         );
    }

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