2013-07-06 31 views
1

Im新的CI/PHP /开发。我坚持这个问题。这似乎是非常基本的,但我不能把它做对!Codeigniter View仅返回最后一行查询

的问题是,我认为只会显示查询的最后一行导致

型号:

$this->db->select('Caption,Description');       
$query = $this->db->get_where('Events', array ('User_iduser' => $user_id)); 
$results = $query->result();        

控制器:

$this->load->model('get_events_model'); 
$data['results'] = $this->get_events_model->get_events(); 
$this->load->view('main/members_area_form',$data); 

查看:

<?php foreach($results as $row); { 
echo $row->Caption; 
echo $row->Description; 
} 
?> 

要麻烦您排除我已经把:

var_dump($results); 

...视图,其结果是:

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" } } 

所以没有什么不对的分贝查询,但仍foreach循环中只显示最后一行来自查询。我究竟做错了什么?

回答

4
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. 
+0

之后是仅此而已。它现在的工作,这一切都是有道理的。谢谢 –

0

看看你的视图代码:

你应该删除您不必要的分号“;” 在foreach

<?php 
    foreach($results as $row) { 
     echo $row->Caption; 
     echo $row->Description; 
    } 
?>