1

我是 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 循环仍然只显示查询的最后一行。我究竟做错了什么?

4

2 回答 2

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.
于 2013-07-06T21:33:50.160 回答
0

查看您的视图代码:

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

<?php 
     foreach($results as $row) {
        echo $row->Caption;
        echo $row->Description;
     }
?>
于 2013-07-06T21:32:52.820 回答