未定义的变量:结果


Undefined variable: results

  1. 型号

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

2.控制器

    public function inf()
    {
        $this->load->model('addm');
        $data['results'] = $this->addm->show($query);
        $this->load->view('backend/first',$data);
    }

3.查看

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

<?php
echo"<td>";
if (is_array($result)) {
   foreach ($result() as $row) {
       $id = $row->id;
       $words = $row->words;
      } 
}
echo "</td>"; 
?>

很少有东西,

  • 在您的模型中,函数show不是期望任何参数
  • 根据需要从$this->addm->show();中删除$query
  • 在视图中,变量应该是结果而不是效果

    // 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();
    }