1

I am new to CodeIgniter so I'm just trying to create a pretty basic site. I have 4 controllers/pages that I want to load, and a possibility to add a few more.

I have an array of items in my /applications/config/site.php file (which is autoloaded) as shown:

$site['MenuItems']['Home'] = "http://mysite.com/site/home";
$site['MenuItems']['Network Info'] = "http://mysite.com/site/info";
$site['MenuItems']['Staff'] = "http://mysite.com/site/staff";
$site['MenuItems']['Support'] = "http://mysite.com/site/support";

$config['site'] = $site;

I want to be able to take the $site['MenuItems'] array and echo out key/value pairs to place ultimately into my view page so that they are displayed as links on my site in the header. I want to be able to add and subtract items from this $site['MenuItems'] array as I need to to create more links in my header.

For example, in my view if I were to echo out the 'Home' => "http://mysite.com/site/home" key value pair:

<li>
   <a href="http://mysite.com/site/home">Home</a>
</li>

I'm not sure if I use $this->config->load('site','MenuItems') to do this...or what?

Thanks for any help you can provide me. Let me know if I'm missing something. It's probably something incredibly easy and I just can't grasp it right now :(

Jguy
  • 583
  • 1
  • 11
  • 33
  • 1
    Looks like a duplicate to me: http://stackoverflow.com/questions/2631439/codeigniter-accessing-config-variable-in-view – Grilse Feb 05 '12 at 20:32

2 Answers2

1

Controller's code:

$data['MyVarsArray'] = "That's my menu!";
$data['MyLinks'] = $this->config->item('site');
$this->load->view('myview',$data);

myview.php code:

<h2><?=$MyVarsArray?></h2>

<ul>
<?php

foreach($MyLinks['MenuItems'] as $key=>$value){?>

<li>
    <a href="<?=$value?>"><?=$key?></a>
</li>

<?}

?>
</ul>
Egor Sazanovich
  • 4,979
  • 5
  • 23
  • 37
  • With that, I'm getting: Invalid Argument supplied for foreach. I echo $this->config->item('MenuItems'); just by itself and get nothing back. – Jguy Feb 05 '12 at 21:14
0

try this

Controller's code:

$data['MyVarsArray'] = "That's my menu!";
$data['MyLinks'] = $this->config->item('MenuItems');
$this->load->view('myview',$data);

myview.php code:

<h2><?=$MyVarsArray?></h2>

<ul>
<?php

foreach($MyLinks as $key=>$value){?>

<li>
    <a href="<?=$value?>"><?=$key?></a>
</li>

<?}

?>
</ul>
Dau
  • 8,578
  • 4
  • 23
  • 48