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

1

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

With that, I'm getting: Invalid Argument supplied for foreach. I echo $this->config->item('MenuItems'); just by itself and get nothing back.
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>

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.