CodeIgniter-Chinese / CodeIgniter

开源 PHP 框架 CodeIgniter 中国社区分支
http://codeigniter.org.cn/
MIT License
65 stars 26 forks source link

view can not get data! #2

Closed snopein closed 10 years ago

snopein commented 11 years ago

CI V2.1.3 PHP V5.2.5


//controller------------------------------------------------------------------------------------
    public function index()
    {
        $data = array(
                'blog_title'   => 'My Blog Title',
                'blog_heading' => 'My Blog Heading',
                'blog_entries' => array(
                      array('title' => 'Title 1', 'body' => 'Body 1'),
                      array('title' => 'Title 2', 'body' => 'Body 2'),
                      array('title' => 'Title 3', 'body' => 'Body 3'),
                      array('title' => 'Title 4', 'body' => 'Body 4'),
                      array('title' => 'Title 5', 'body' => 'Body 5')
                 )
            );

         $this->load->view('welcome_message',$data);
    }
// END----------------------------------------------------------------------------------------


//view---------------------------------------------------------------------------------------



//END--------------------------------------------------------------------------------------

ERROR Notice

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Lamtin commented 11 years ago

See https://github.com/CodeIgniter-Chinese/CodeIgniter/blob/develop/system/core/Loader.php#L858 and http://www.php.net/manual/en/function.extract.php

snopein commented 11 years ago

OH! Thank you very much, but why CodeIgniter Manual did not remind.

He should be added.

Lamtin commented 11 years ago

http://ellislab.com/codeigniter/user-guide/general/views.html

Let's try it with your controller file. Open it add this code:

<?php
class Blog extends CI_Controller {

        function index()
        {
            $data['title'] = "My Real Title";
            $data['heading'] = "My Real Heading";

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

Now open your view file and change the text to variables that correspond to the array keys in your data:

<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
    <h1><?php echo $heading;?></h1>
</body>
</html>