Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Codeigniter视图仅返回最后一行查询_Codeigniter_Codeigniter 2 - Fatal编程技术网

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

Codeigniter视图仅返回最后一行查询,codeigniter,codeigniter-2,Codeigniter,Codeigniter 2,我是CI/PHP/development的新手。我被这个问题难住了。这似乎是真正的基本,但我只是不能得到它的权利 问题是我的视图将只显示查询结果的最后一行 型号: $this->db->select('Caption,Description'); $query = $this->db->get_where('Events', array ('User_iduser' => $user_id)); $results

我是CI/PHP/development的新手。我被这个问题难住了。这似乎是真正的基本,但我只是不能得到它的权利

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

型号:

$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);
视图:

…在视图中,结果是:

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

查看您的视图代码:

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

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

查看您的视图代码:

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

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

从中删除“;”
从中删除“;”

是的,就是这样。它现在起作用了,这一切都是有意义的。谢谢是的就是这样。它现在起作用了,这一切都是有意义的。非常感谢。
<?php 
     foreach($results as $row) {
        echo $row->Caption;
        echo $row->Description;
     }
?>
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.