Codeigniter视图仅返回最后一行查询

时间:2013-07-06 21:24:48

标签: codeigniter codeigniter-2

我是CI / PHP /开发的新手。我坚持这个问题。这似乎是非常基本的,但我无法做到正确!

问题是我的视图只会显示查询结果的最后一行

型号:

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

因此db查询没有任何问题,但foreach循环仍然只显示查询的最后一行。我做错了什么?

2 个答案:

答案 0 :(得分:1)

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.

答案 1 :(得分:0)

查看您的观看代码:

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

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