Reputation: 13
Im new to CI/PHP/development. Im stuck on this problem. It seems to be really basic but i just cant get it right!
The problem is that my view will only display the last row of the query results
Model:
$this->db->select('Caption,Description');
$query = $this->db->get_where('Events', array ('User_iduser' => $user_id));
$results = $query->result();
Controller:
$this->load->model('get_events_model');
$data['results'] = $this->get_events_model->get_events();
$this->load->view('main/members_area_form',$data);
View:
<?php foreach($results as $row); {
echo $row->Caption;
echo $row->Description;
}
?>
To troubleshoot I have put:
var_dump($results);
...in the view and the result is:
array(3) { [0]=> object(stdClass)#18 (2) { ["Caption"]=> string(5) "Color" ["Description"]=> string(4) "Blue" } [1]=> object(stdClass)#19 (2) { ["Caption"]=> string(5) "Color" ["Description"]=> string(3) "Red" } [2]=> object(stdClass)#20 (2) { ["Caption"]=> string(5) "Color" ["Description"]=> string(5) "Black" } }
So there is nothing wrong with the db query but still the foreach loop is only displaying the last row from the query. What am I doing wrong?
Upvotes: 1
Views: 1303
Reputation: 889
remove ";" from
<?php foreach($results as $row); {
the line will be
<?php foreach($results as $row) {
in your case foreach was only looping but your echos was not in foreach.
after executing foreach
it was coming to your echo part and echoing last row as foreach already conpleted its loop.
Upvotes: 1
Reputation:
look at your view code:
you should remove your unnecessary semicolon ";" after the foreach
<?php
foreach($results as $row) {
echo $row->Caption;
echo $row->Description;
}
?>
Upvotes: 0