【问题标题】:I can't pass the data from the controller to the view in CodeIgniter 3我无法将数据从控制器传递到 CodeIgniter 3 中的视图
【发布时间】:2022-01-05 01:11:14
【问题描述】:

我无法将数据从控制器传递到 CodeIgniter 3 中的视图。一切看起来都还不错。我需要把Controller的部分放在index()方法中吗?

Donor_Model.php

function viewDonors() {
        $query = $this->db->get('donors');
        return $query;
    }

Staff.php 控制器

public function viewDonors()
    {

        $this->load->Model('Donor_Model');
        $data['donors'] = $this->Donor_Model->viewDonors();
        $this->load->view('viewdonors', $data);
    }

当我尝试在 viewdonors.php 视图中调用 $data 时,它会将 $donors 显示为未定义。

screenshot of viewdonors.php

【问题讨论】:

  • 您的 IDE 将其显示为“未定义”。如果你真的运行它会发生什么。

标签: php codeigniter codeigniter-3


【解决方案1】:

@TimBrownlaw 是对的。即使 IDE 显示错误,代码也可以正常运行。

【讨论】:

    【解决方案2】:

    您需要将模型页面编辑为:-

    function viewDonors() {
        $query = $this->db->get('donors');
        return $query->result();
    }
    

    由于您想在视图页面中循环访问捐赠者数据,因此您需要在模型中返回结果函数。

    【讨论】:

      猜你喜欢
      • 2016-06-25
      • 2011-09-09
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      相关资源
      最近更新 更多