未定义的变量:结果

时间:2015-09-26 18:03:29

标签: php codeigniter undefined

  1. 模型

    public function show()
    {
        $query = $this->db->get('text');
        return $query->result();
    }
    
  2. 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>"; 
    ?>
    

1 个答案:

答案 0 :(得分:1)

很少,

  • 在您的模型中,功能展示不是 期待 任何参数
  • 根据需要从$query移除$this->addm->show();
  • 在视图变量中,结果不是结果

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