未定义的变量:结果

Undefined variable: results

  1. 型号

    public function show()
    {
        $query = $this->db->get('text');
        return $query->result();
    }
    

2.Controller

    public function inf()
    {
        $this->load->model('addm');
        $data['results'] = $this->addm->show($query);

        $this->load->view('backend/first',$data);

    }

3.view

我正在尝试从我的数据库中获取数据,但我无法解决此错误: 未定义的变量:结果

<?php
echo"<td>";

if (is_array($result)) {
   foreach ($result() as $row) {
       $id = $row->id;
       $words = $row->words;
      } 
}

echo "</td>"; 
?>

几件事,

  • 在你的模型函数中显示的不是预期任何参数
  • 根据需要从 $this->addm->show(); 中删除 $query
  • 在视图变量中应该是results而不是result

    // controller
    public function inf()
    
    {
        $this->load->model('addm');
        $data['results'] = $this->addm->show();
        $this->load->view('backend/first', $data);
    }
    
    
    // view
    if (!empty($results)) {
        foreach($results as $row) {
            $id = $row->id;
            $words = $row->words;
        }
    }
    
    // model
    public function show()
    
    {
        $query = $this->db->get('text');
        return $query->result();
    }