0

I am trying to pass an array and use it in my view from my controller but instead i am getting some errors...

In my Controller:

$data = array(
  'a' => 'b',
  'c' => 'd'
);
$this->load->view('home/index', $data);

In my View:

print_r($data);

throws errors and doesnt allow me to print it, since i am trying to then pass the array to another view for my app.

Error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: home/index.php

Line Number: 1

3 Answers 3

3

Codeigniter will create variables named with the keys of every item in your data array.

If you want them all in one data array accessible in your view, try that:

$data = array(
  'data' => array(
       'a' => 'b',
       'c' => 'd'
  ) 
);
Sign up to request clarification or add additional context in comments.

Comments

0

In the view, try accessing as $a and $c rather than $data

Comments

0

$data is just a variable and has nothing to do with the names of variables accessible by the view.

you won't use $data, will use $a and $c because they are the keys of your array values.

Take a look at Codeigniter documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.