2013-07-06 35 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; 
    } 
?>