Welcome Guest, Not a member yet? Register   Sign In
Loading array into view, assign to var, not working
#1

[eluser]RJ[/eluser]
Hello,

I'm trying to load an array into a view:
Code:
$data['content']    =    $this->load->view('test',$fields, TRUE);

In the view I have
Code:
foreach ($fields as $field)
{
  print_r($field);
}

Now I know $fields is supposed to be $fields['field_name'], BUT, $fields isn't passing at all.
Quote:Message: Undefined variable: fields
Filename: libraries/Loader.php(673) : eval()'d code
Line Number: 8

Can anyone see where I am going wrong?

Thanks
#2

[eluser]jedd[/eluser]
$fields doesn't exist within the view.

The contents of the array $fields does exist.

If $fields isn't an array, you won't find anything either.

If you want $fields to go across, then in the controller do this:
$bob['fields'] = $fields

$this->load->view ( 'test' , $bob , TRUE);
#3

[eluser]bretticus[/eluser]
Hmmm...

To me it looks like you are loading the contents of a view into an element of the $data array with the key of "content". I assume you have declared $fields above the first line of code you're showing?
#4

[eluser]RJ[/eluser]
@jebb
you are the awesome! it makes sense now CI makes those children of the $field array available in the view as variables like $name. But not the entire array, you have to set it into an array to use as array; children of $fields if array will be available as array in view.

That right?
#5

[eluser]jedd[/eluser]
I am simultaneously flattered and disturbed that you're describing George's younger brother as 'the awesome'.

Nonetheless, parsing your assessment as best I can, I think you have it. But allow me to precis the situation.

You must pass an array to a view.

That array can, as is usual for an array, contain any number (and type) of variables, including other arrays.

Every element in the array you pass will then appear as its own variable within the view, extracted, as it were, from the original array.

Passing this:
Code:
$data['captain'] = "Said what";
$this->load->view ("test" , $data  );
Will result in the variable $captain existing in your view, with the value of "Said what"
#6

[eluser]RJ[/eluser]
lmao! :coolcheese: took me a hot minute but i got it.
#7

[eluser]Colin Williams[/eluser]
The magic is done with http://php.net/extract if you want to fully understand it. Also, I believe CI typecasts your $data param, so you can send it an object, too.




Theme © iAndrew 2016 - Forum software by © MyBB