消息:视图中的未定义变量

时间:2018-03-27 06:08:29

标签: codeigniter

我试图在codeigniter中查看我的视图中的数据,但它给了我一条消息:未定义的变量:topics_array

这是我的模特:

public function studentTopics($courseId, $periodId)
{
    $this->dbi->select('topicitem.TopicItemID, topicItem.Content');
    $this->dbi->from('topicitem, topics, periods');
    $this->dbi->where('topics.PeriodItemID', $periodId)
            ->where('topics.TopicItemID = topicitem.TopicItemID')
            ->where('topics.PeriodItemID = periods.PeriodItemID')
            ->where('periods.CourseID', $courseId);
    $res = $this->dbi->result_array(); 

    return $res;
}

这是我的控制器

public function student_topic($courseId, $periodId)
{
   $this->load->library('table');  
   $this->load->model('Student_model');      
   $data['topics_array'] = $this->Student_model->studentTopics($courseId, $periodId);  
   $this->load->view('Student/StudentView', $data);
}

和我的观点

<?php
  $courseID = $this->uri->segment(4);
  $periodID = 1;
  foreach ($topics_array as $key =>$value) {
       echo "<tr> 
             <td>".$value['Content']. "</td>
             </tr>"; }?>

2 个答案:

答案 0 :(得分:0)

您似乎错过了get()

$res = $this->dbi->get()->result_array(); 

return $res;

答案 1 :(得分:0)

希望这会对你有所帮助:

更改此行代码:$res = $this->dbi->result_array();

$this->dbi->select('topicitem.TopicItemID, topicItem.Content');
$this->dbi->from('topicitem, topics, periods');
$this->dbi->where('topics.PeriodItemID', $periodId)
        ->where('topics.TopicItemID = topicitem.TopicItemID')
        ->where('topics.PeriodItemID = periods.PeriodItemID')
        ->where('periods.CourseID', $courseId);
$query = $this->dbi->get(); // missing this line of code

$res = $query->result_array();

了解更多:https://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-specific-data