Codeigniter: コントローラからビューにデータを渡す 質問する

Codeigniter: コントローラからビューにデータを渡す 質問する

$dataという名前のコントローラーから にpoll渡したいのですresults_viewが、未定義の変数エラーが発生します。

<?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);

    }
}

結果ビュー.php

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

ベストアンサー1

$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);

結果ビュー.php

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

おすすめ記事