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

[eluser]xwero[/eluser]
[quote author="kriztofor" date="1227666885"]$includes is not an associative array, so you can't use $key => $val. Try
Code:
<?php
foreach ($includes as $val){
    echo "$key\n";
}
?>
[/quote]
This is a wrong assumption. Every array item has a key, an associative array has words as keys where a normal array has numeric keys.
#12

[eluser]kriztofor[/eluser]
[quote author="xwero" date="1227705869"]This is a wrong assumption. Every array item has a key, an associative array has words as keys where a normal array has numeric keys.[/quote]

I stand corrected.
#13

[eluser]John_Betong[/eluser]
 
Try this in your original installation
 
Modify your ./controller/welcome.php
Code:
if (FALSE)
        {
          // Original Welcome.php code  
          $this->load->view('welcome_message', $data);
        }else{  
          // Moidifed script
          $scripts  = $data;
          $scripts['includes']=array('superfish','layout', 'admin');
          
          $this->load->view('welcome_message', $scripts);
        }
 
Include this NEW STUFF in your ./views/welcome.php
Code:
<p><br />Page rendered in {elapsed_time} seconds</p>

  <div style='color:#f00; font-weight:bold'>
    &lt;?php
      echo 'NEW STUFF START';
      echo '<pre>';
        print_r($includes);
      echo '</pre>';
      echo 'NEW STUFF END';
    ?&gt;
  </div>  


  &lt;/body&gt;
  &lt;/html&gt;
&nbsp;
&nbsp;
#14

[eluser]Dr. Seuss[/eluser]
Whew. Back on this; sorry for the delay...

I have tried numerous things, both in the view and controller; everything is returning the same error "Message: Undefined variable: scripts" or "Message: Undefined variable: includes"...


Code:
&lt;?php
class Designmenu  extends Controller {

     var $scripts['includes']=array('superfish','layout', 'admin');

    function __construct()
    {
        parent::Controller();    
    }


    function index()
    {
    $this->load->model('siteignite/treejson');        
    $data['mnujson'] = $this->treejson->get_json();
     if(strlen($data['mnujson'])>0){
        $this->load->view('common/header', $scripts);
        $this->load->view('common/menu', $data);

        $this->load->view('siteignite/body');

        $this->load->view('common/col2');
        $this->load->view('common/col3');
         $this->load->view('common/footer');

        }
        
    }
}?&gt;


...and in the view...

Code:
&lt;?php

echo var_dump($scripts['includes']);

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

?&gt;

The resulting germane errors are:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: scripts

Filename: common/header.php

Line Number: 10
NULL
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: includes

Filename: common/header.php

Line Number: 12
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: common/header.php

Line Number: 12

Truly confusing.

BTW, I did try a variant of the "$scripts = $data" code, but that got the same result.
#15

[eluser]JeremyApp[/eluser]
Don't use the array in your view. When you do this in your controller:

Code:
$scripts['includes'] = array('xxxxx', 'yyyyy');

$this->load->view('xxxxx', $scripts);

You should access it like this in your view:

Code:
print_r($includes);

When you pass a variable to your view, the key is the variable that you can access from it - for example, $scripts['test'] in my controller would be $test in my view.

You're trying to load $scripts['includes'] in your view, but that's not the variable being passed there - check out the user guide on views to see how it all works.
#16

[eluser]Dr. Seuss[/eluser]
Thanks, but I tried that.

Code:
&lt;?php

print_r($includes);

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

...gives...

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: Notice

Message: Undefined variable: includes

Filename: common/header.php

Line Number: 12
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: common/header.php

Line Number: 12

Maybe I'm making this too complex, so let me try asking a different way....

What would I have to do in order to change this (which works):

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

...to...

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

I suspect that it has something to do with the delcarations and scope of $data, but I have not been able to figure it out.
#17

[eluser]JeremyApp[/eluser]
Why aren't you using $data?
#18

[eluser]Dr. Seuss[/eluser]
Is this a bad design practice?

In my mind I was separating the arrays one for "display data" and the other for "application logic" data.

In the case that I am working with I was wanting to specify a "scripts" variable that could have "includes" and "functions" within it. when I sent this to my "header" view, it would only package what was necessary for that page.

I am certainly no MVC--or any other design pattern--expert, so this is stupid, please let me know.




Theme © iAndrew 2016 - Forum software by © MyBB