Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

php - Codeigniter passing data from controller to view

As per here I've got the following controller:

class User extends CI_Controller {
    public function Login()
    {
        //$data->RedirectUrl = $this->input->get_post('ReturnTo');
        $data = array(
               'title' => 'My Title',
               'heading' => 'My Heading',
               'message' => 'My Message'
          );
        $this->load->view('User_Login', $data);
    }

    //More...
}

and in my User_Login.php view file I do this:

<?php print_r($data);?>

which results in:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/User_Login.php
Line Number: 1

Do I need to load any specific modules/helpers to get the $data variable populated? If I print_r($this), I can see a lot of stuff but none of my data except in caches

Edit: To clarify, I know that calling the variable the same in the controller and view won't "share" it - it's out of scope but in the example I linked, it seems to imply a $data variable is created in the scope of the view. I simply happened to use the same name in the controller

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ah, the $data array's keys are converted into variables: try var_dump($title); for example.

EDIT: this is done using extract.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...