nady gold
nady gold

Reputation: 109

unable to display array result

I got the following array. now I get the single records but when is their array records then it doesn't work. I tried with OR condition but it doesn't work.

$this->db->get_where('genre',array('genre_id'=>$row['genre_id']))->row()->name;
//I get Follwoing Records
Array(
[0] => Array
    (
        [movie_id] => 7
        [title] => Raaz
        [genre_id] => 8 // it display the name
        [actors] => []
        [trailer_url] => https://drive.google.com/
    )
[1] => Array
    (
        [movie_id] => 8
        [title] => Tribute
        [genre_id] => ["2","5","20"] // it doesn't display the name
        [actors] => []
        [trailer_url] => https://drive.google.com/
    )

I tried the following code

$this->db->get_where('genre',array('genre_id'=>$row['genre_id']))->row()->name;

above code works for 0 index but it doesn't work 1 index array

Upvotes: 1

Views: 44

Answers (1)

devpro
devpro

Reputation: 16117

You can use where_in but you can't use it with get_where, you need to use alternate here instead of get_where:

Example:

You can alternate here like:

$this->db->select('name');
$this->db->from('genre');
if(is_array($row['genre_id'])){ // if result is in array
    $this->db->where_in('genre_id',$row['genre_id']);    
}
else{ // for single record.
    $this->db->where('genre_id',$row['genre_id']);
}
$query = $this->db->get();
print_r($query->result_array()); // will generate result in an array

Edit:

After debugging, you are getting this value ["2","5","20"] as a string, so you can modify this code:

$genreID = intval($row['genre_id']); // 
if($genreID > 0){ 
    $this->db->where('genre_id',$row['genre_id']);    
}
else{ 
   $genreID = json_decode($row['genre_id'],true);
   $this->db->where_in('genre_id',$genreID);    
}

CI Query Builder

Upvotes: 1

Related Questions