2

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?

1
  • You shouldn't make two trips to the database, use a JOIN. Commented Sep 8, 2024 at 9:47

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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();

Comments

0

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 E.g

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.