I have a class system and a function inside it that does a foreach from DB results. The variable is assigned inside the foreach, but outside of the foreach it is empty.
// Top of file
private $movieList = array();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
// add each to the array
$this->movieList[] = array('nid' => $row->nid, 'title' => $row->title, 'movie_pos_id' => $row->movie_pos_id);
print_r($this->movieList); // variable full of stuff
}
// No results found
return false;
}
print_r($this->movieList); // variable empty
Any idea why?
Looks to me like your line outside the if statement would never execute because you always return false if you got results. Check your brackets. You probably mean this: