首页 文章

将变量从控制器传递到CodeIgniter中的视图

提问于
浏览
15

我是Codeigniter和OOP PHP的新手 .

控制器:

public function index(){
    $this->load->model('main_model');
    $planet = $this->main_model->solar();
    $this->load->view('main_view', $planet);    
    }

如果在控制器中 echo $planet 它会做它应该做的事情 . 如果我在视图中 echo $planet 我得到一个未定义的变量错误 . $planet 不是数组 . 为什么不将 $planet 变量传递给视图?

我知道这是一个简单而基本的问题,我很尴尬,我无法弄清楚我做错了什么 .

编辑:好的,经过更多的摆弄,我得到了它的工作 . 变量只有在被格式化为数组时才能从Controller传递到View?

7 回答

  • 1

    您必须将数组传递给视图 . CodeIgniter会自动为您提供 $planet .

    $data = array('planet' => $planet);
    $this->load->view('main_view', $data);
    

    有了这个,您可以在视图中使用 $planet .

    例如,如果您执行以下操作:

    $data = array('foo' => 'Hello', 'bar' => 'world');
    $this->load->view('main_view', $data);
    

    您的视图中将显示 $foo$bar . 数组中的键值对会自动转换为视图中的变量 .

  • 1

    您可以将数组或对象传递给视图 . 然后,您可以在视图中将数组键作为变量访问 .

    控制器

    Array

    public function index()
    {
        $this->load->model('main_model');
        $planet_data['planet'] = $this->main_model->solar();
        $this->load->view('main_view', $planet_data);
    }
    

    Object

    public function index()
    {
        $this->load->model('main_model');
        $planet_data = new stdClass(); //Creates a new empty object
        $planet_data->planet = $this->main_model->solar();
        $this->load->view('main_view', $planet_data);
    }
    

    CodeIgniter's user manual:注意:如果使用对象,则类变量将转换为数组元素 .

    查看

    无论您如何传递数据,都可以显示如下:

    <?php echo $planet; ?>
    

    如果它是一个数组,那么你需要迭代它 . 或者一个对象,然后访问它的成员变量 .


    根据我的经验,使用数组比使用对象更常见 .

  • 1

    这是如何轻松完成的

    public function index() {
        $this->load->model('main_model');
        $data['planet'] = $this->main_model->solar();
        $this->load->view('main_view', $data);    
    }
    

    现在在您的视图中,您可以通过这种方式访问该值

    echo $planet;
    
  • 0

    试试:

    public function index(){
        $this->load->model('main_model');
        $data['planet'] = $this->main_model->solar();
        $this->load->view('main_view', $data);    
    }
    

    并且在你看来你可以访问“$ planet” . 就是这样 . 如果它有用,则接受答案

  • 12

    你可以在url中传递一个变量

    function regresion($value) {
    
        $data['value'] = $value;
        $this -> load -> view('cms/template', $data);
    }
    

    在视图中

    <?php print_r($value);?>
    
  • 27

    如果modal包含数组响应:

    public function solar() {
       $data['earth'] = 'Earth';
       $data['venus'] = 'Venus';
       return $data;
    }
    

    然后你要查看的数据如下:

    public function index(){
        $this->load->model('main_model');
        $planet = $this->main_model->solar();
        $this->load->view('main_view', $planet);    
    }
    

    但是在视图中你将拥有这样的访问数据:

    echo $earth;
    echo $venus;
    

    如果您不知道想要响应,那么请使用如下代码:

    public function index(){
        $this->load->model('main_model');
        $planet['planet'] = $this->main_model->solar();
        $this->load->view('main_view', $planet);    
    }
    

    在视图文件中,您可以访问如下数据:

    print_r($planet);
    

    If you don't know what data will come into view then just use this code at view:

    print_r($this->_ci_cached_vars);
    
  • 6

    在视图中我们传递数组并且数组的键将自动变为变量并且将由codeigniter提供给你,我们不能简单地传递变量来查看我们需要传递数组

    以下是您的代码

    public function index(){
      $this->load->model('main_model');
      $planet = $this->main_model->solar(); // <--- Change this line 
      $this->load->view('main_view', $planet);    
    }
    

    只需将其更改为

    public function index(){
      $this->load->model('main_model');
      $data['planet']= $this->main_model->solar(); // <--- to this
      $this->load->view('main_view', $data);    
    }
    

    在您的视图中,您可以像这样简单地访问它

    echo $planet;
    

相关问题