Welcome Guest, Not a member yet? Register   Sign In
New Associative Array?
#1

[eluser]Dr. Seuss[/eluser]
I feel extremely foolish, but here goes: I want to create custom array variables. I try:

Code:
$scripts['includes']=array('superfish','layout', 'admin');
    $this->load->vars($scripts);

And then pass the variable to a view:

Code:
$this->load->view('common/header', $scripts);

In the view I have tried both

Quote:<?php

foreach ($scripts as $key => $val) {
echo "$key = $val\n";
}

?>

...and...

Quote:<?php

foreach ($scripts['includes'] as $key => $val) {
echo "$key = $val\n";
}

?>


In both cases, I get this error message:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: scripts

Filename: common/header.php

Line Number: 10

Just trying not to place everything within the "$data" array; I know that this must be possible and that I am missing something simple, but I cannot figure it out after two hours of flailing...any help would be appreciated.
#2

[eluser]xwero[/eluser]
try $includes Wink
#3

[eluser]Dr. Seuss[/eluser]
I chanced it to read:

Code:
<?php

    foreach ($includes as $key => $val) {
        echo "$key = $val\n";
    }

?>

...and received the following error message:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: includes

Filename: common/header.php

Line Number: 10
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: common/header.php

Line Number: 10

Crazy stuff....
#4

[eluser]Jamie Rumbelow[/eluser]
Don't call the $this->load->vars() function - CodeIgniter will do that automatically.
#5

[eluser]Dr. Seuss[/eluser]
Alright, removed the line "", so it now reads thus:

Code:
$scripts['includes']=array('superfish','layout', 'admin');
$this->load->view('common/header', $scripts);

..and I returned the "view" code to read:

Code:
<?php

    foreach ($scripts['includes'] as $key => $val) {
        echo "$key = $val\n";
    }

?>

...also tried:

Code:
<?php

    foreach ($scripts as $key => $val) {
        echo "$key = $val\n";
    }

?>

...and in desperation:

Code:
<?php

    foreach ($includes as $key => $val) {
        echo "$key = $val\n";
    }

?>
..and we are now back to the original error:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: scripts

Filename: common/header.php

Line Number: 10
#6

[eluser]xwero[/eluser]
The way load->vars or the second parameter of load->view works is you add an associate array and the key will be the name of the variable. With your example includes should be the name of the variable in your view.

Maybe the error is caused by adding the array to both vars and view method.
#7

[eluser]Dr. Seuss[/eluser]
Thank you for the reply; I realize that I did not explicitly state in the previous message that I had deleted the line "$this->load->vars() function". But the error is still present even though I am only using:

Code:
$scripts['includes']=array('superfish','layout', 'admin');
$this->load->view('common/header', $scripts);

...and in the view:

Code:
<?php

    foreach ($includes as $key => $val) {
        echo "$key = $val\n";
    }

?>

I still get this error:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: includes

Filename: common/header.php

Line Number: 10
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: common/header.php

Line Number: 10
#8

[eluser]kriztofor[/eluser]
$includes is not an associative array, so you can't use $key => $val. Try
Code:
<?php
foreach ($includes as $val){
    echo "$key\n";
}
?>
#9

[eluser]Dr. Seuss[/eluser]
Ahhh...good catch, but when I correct to this:

Code:
<?php

    foreach ($includes as $val) {
        echo $val."\n";
}

?>

I still get these errors:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: includes

Filename: common/header.php

Line Number: 10
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: common/header.php

Line Number: 10
#10

[eluser]simshaun[/eluser]
In your view, try this before the loop and see if it is outputting the array.
Code:
echo var_dump($includes);

If not, and there is no other code in your view or controller that would change $includes, then this basic code should work just fine:

Controller ->
Code:
$scripts['includes'] = array('superfish', 'layout', 'admin');
$this->load->view('common/header', $scripts);

View ->
Code:
foreach ($includes AS $val) {
    echo $val."\n";
}




Theme © iAndrew 2016 - Forum software by © MyBB