0

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

3
  • check function or_where_in() Commented Mar 28, 2019 at 15:38
  • @Mohammad: but it will not work with get_where, Commented Mar 28, 2019 at 15:46
  • 1
    You aren't executing one query, then executing one or more queries based on the result set of the first, right? Commented Mar 5 at 6:45

1 Answer 1

1

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

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

7 Comments

it works fine with single data but it doesn't work with array values
@nadygold: what are u getting?
for single i get Array ( [0] => Array ( [name] => Comedy ) ) but in array i get blank
i just print the query that i get following for array SELECT name FROM genre WHERE genre_id = '[\"2\",\"5\",\"20\"]'
run this query in your phpmyadmin and chk if u r getting result SELECT name FROM genre WHERE genre_id IN (2,5,20) @nadygold
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.