如何将数据从Controller传递给View?

时间:2017-10-15 16:48:14

标签: php codeigniter

我是新手且低于平均水平的学生,但我并不完全在这个Codeigniter我面临数据通过问题,有人请用一个简单的方法帮助我。

控制器

MATCH p=(b:Book {id: "xxx"})-[:HAS_CATEGORY]->(c:Category)<-[:HAS_CATEGORY]-(o:Book) WHERE o <> b
RETURN p

查看

public function View_data(){
      $this->load->model('student_model');
      $data["fetch_data"]=$this->student_model->view_student();
      $this->load->view(student_view,$data);
}

4 个答案:

答案 0 :(得分:4)

控制器应该是

$this->load->view('student_view',$data); # wrap view name with quotes

您正在传递数组。所以你必须foreach 循环它,或者通过数组的索引调用

foreach ($fetch_data as $key => $item) {
    echo $item->element; # or  $item['element']
}

如果使用索引,那么

$fetch_data[0]['element']
  

访问方法将与您在模型中获取数据的方式不同

答案 1 :(得分:1)

我认为视图名称student_view应该在引号内。

$this->load->view(student_view,$data);

$this->load->view('student_view',$data);

这可能是$data未传递给视图的原因。

答案 2 :(得分:1)

视图上的

就像这样

<?php 
    foreach($fetch_data as $fd) {
     echo $fd->name;
}
?>

答案 3 :(得分:0)

你可以这样做:

 $this->load->view(student_view,['fetch_data' => $fetch_data]);


public function View_data(){
      $this->load->model('student_model');
      $fetch_data = $this->student_model->view_student();
      $this->load->view(student_view,['fetch_data' => $fetch_data]);
}

To check the data on view:

echo '<pre>'; print_r($fetch_data); echo '</pre>';

如果你吃了传递数组表单模型然后在视图中你可以尝试这样:

在数组

echo $fetch_data[0]['your_column']; //If there is multiple row 
echo $fetch_data['your_column']; //If there is single row 

在stdClass对象

echo $fetch_data[0]->your_column; //If there is multiple row 
echo $fetch_data->your_column; //If there is single row