I am getting error message: Invalid argument for foreach() in my View. I wanted to display all entries in my mysql table but i kept on getting error message. I am a newbie i
The query in your model is returning either string or object data, instead of an array. The 'foreach' loop function accepts an array as its first argument.
I think this code modular:
public function getAll() {
$this->db->select('bcode, bname, btel, badd');
$this->db->from('branches');
$query = $this->db->get('branches');
return $this->db->query($query)->result_array();
}
you need to do a check before starting to iterate for the data, like: model code:
public function getAll() {
$results = array();
$this->db->select('bcode, bname, btel, badd');
$this->db->from('branches');
$query = $this->db->get();
if($query->num_rows() > 0) {
$results = $query->result();
}
return $results;
}
view code:
if( !empty($results) ) {
foreach($results as $row) {
echo '<tr>';
echo '<td>'.$row->bcode.'</td>';
echo '<td>'.$row->bname.'</td>';
echo '<td>'.$row->btel.'</td>';
echo '<td>'.$row->badd.'</td>';
echo '</tr>';
}
}