1

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

I'm trying to pass both session data and a db query to a view in Codeigniter. I get an error in my view file that says: 'Message: Undefined variable: data'

The problem lies with in the $idea variable. The variables register an undefined error.

Controller:

public function members() {

    if($this->session->userdata('is_logged_in')){
        $session = $this->session->all_userdata();
        $facebook_id = $data['id']; 


        $this->load->model('idea'); 
        $idea = $this->idea->get_idea($facebook_id); 

        //merge the two arrays to get data variable
        $data = $session + $idea; 
        $this->load->view('members', $data); 
    } else {
        redirect('main/login'); 
    }
}

Model:

public function get_idea($facebook_id) {
    $data = array(
        'id' => '', 
        'idea' => '', 
        'facebook_id' => $facebook_id
    );

    $query = $this->db->get('idea', $facebook_id); 

    return $query->result_array(); 

}

Here are some of the resources I've used:
-Creating foreach loops using Code Igniter controller and view
-codeigniter: pass array from controller to view
-http://blog.mattheufarmer.com/2010/building-a-website-in-codeigniter-part-ii-getting-data-and-using-it/

Thank you for the help!

Community
  • 1
  • 1
Michael F
  • 65
  • 1
  • 3
  • 6
  • @tereško: How is that a duplicate? The linked question is about basic PHP variable reference, while this question is about passing parameters between MVC layers. – Kaivosukeltaja Jan 03 '13 at 09:28
  • @Kaivosukeltaja , did you read this part: *"I'm trying to pass both session data and a db query to a view in Codeigniter. I get an error in my view file that says: 'Message: Undefined variable: data'"* ? – tereško Jan 03 '13 at 09:29
  • @tereško: Yes I did, it means the variable is not passed correctly to the view. How does the linked question help in this matter? – Kaivosukeltaja Jan 03 '13 at 09:32
  • @Kaivosukeltaja , did you also notice this part `$facebook_id = $data['id'];`? Where did the `$data` come from? What doe the error message say? – tereško Jan 03 '13 at 10:24
  • @tereško: I assumed it's some CodeIgniter related magic as the accepted answer also uses `$data` without initializing. I might be wrong though, as I'm not familiar with CI. – Kaivosukeltaja Jan 03 '13 at 11:24

2 Answers2

3
$data = $session + $idea; 

No. Just no. That's not how you merge the arrays.

A cleaner, more thoughtful approach to do what you want to do:

$data = array( $session, $idea );

Otherwise,

$data = array_merge($session, $idea);

Edit:

You should read this http://ellislab.com/codeigniter/user-guide/general/views.html

Anyway, unless you're doing something completely stupid, you should be able to do this:

$data = array( 'sesssion' => $sesssion, 'idea' => $idea ); 

And pass that through to your view. And then on your view, you can call the session with $session, and idea with $idea. To illustrate:

$data = array('name' => 'josh', 'sex' => 'often');
$this->load->view('welcome', $data);

And then on my view:

<? echo "$name has sex $sex"; // returns josh has sex often ?>

In the views, the keys become variables and the values are accessed by those variables (which were keys in $data). You don't call $data, you call the key.

$data = array('key' => 'value');

Let me know if you have any other issues. :)

Josh Brody
  • 5,153
  • 1
  • 14
  • 25
3

You can do like this-

public function members() {

    if($this->session->userdata('is_logged_in')){
        $data['session'] = $this->session->all_userdata();
        $facebook_id = $data['id']; 


        $this->load->model('idea'); 
        $data['idea'] = $this->idea->get_idea($facebook_id); 

        $this->load->view('members', $data); 
    } else {
        redirect('main/login'); 
    }
}
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90