0

I am passing an array from controller to view in PHP CodeIgniter. Here is my code in controller. $gen is any array containing many values.

foreach ($gen as $value) {
    $moviesbyid['similarmovie'] = $this->main_model->getsimilarmovies($value);
}

$this->load->view('home/check.php', $moviesbyid);

But the above code fill $moviesbyid['similarmovie'] array only for one value of $value. I want that it contain all the values returned from getsimilarmovies($value) for every value of $value. How can i do this? Here is the method in model:

public function getsimilarmovies($gener)
{
    $this->db->limit(4);
    $this->db->like('Genre',$gener);
    $query = $this->db->get('sources'); 
    return $query->result();
}

1 Answer 1

1

You need to create new items in the array as it loops.
Your code just overwrite the same item every iteration.

foreach ($gen as $value) {
    $moviesbyid['similarmovie'][]=$this->main_model- 
    >getsimilarmovies($value);
}

notice the []

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

Comments

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.