Codeigniter:将数据从控制器传递到视图 [英] Codeigniter: Passing data from controller to view

查看:261
本文介绍了Codeigniter:将数据从控制器传递到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将名为 poll 的控制器中的 $ data 传递给 results_view 然而我得到一个未定义的变量错误。

I want to pass $data from the controller named poll to the results_view however I am getting an undefined variable error.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Poll extends CI_Controller {

    public function __construct()
       {
            parent::__construct();
            $this->load->database();
            $this->load->helper('form');
       }

    public function index()
    {

        $this->load->view('poll_view',$data);
    }

    public function vote()
    {
        echo "Voting Successfull";
        $this->db->insert('votes',$_POST);
    }

    public function results()
    {
        echo "These are the results";
        //$query = $this->db->get('votes');
        $data = "hello";
        $this->load->view('results_view', $data);

    }
}

Results_view.php

Results_view.php

<html>
<?php echo $data; ?>
</html>


推荐答案

$ data 应为数组或对象: http://codeigniter.com/user_guide/general/views.html

$data = array(
    'title' => 'My Title',
    'heading' => 'My Heading',
    'message' => 'My Message'
);

$this->load->view('results_view', $data);

results_view.php

<html>
<?php 
//Access them like so
echo $title.$heading.$message; ?>
</html>

这篇关于Codeigniter:将数据从控制器传递到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆