Welcome Guest, Not a member yet? Register   Sign In
Coding approach question: how to (easily) build a navigation.
#1

[eluser]Bramme[/eluser]
Hey all,

First of all, I just started using CodeIgniter (I downloaded it like an hour ago or so :p), so I want to apologize if I'm asking smth really stupid, or smth that has been answered a million times allready. I just don't know how to search for an answer.

I'm trying to port a website I made with Smarty to CI. The website is a very easy portfolio with only a few (more or less) static pages: home, contact, about, paintings, drawings, portraits, guestbook.

At the moment I'm using 5 separate tpl files: one for each page, except for the paintings/portraits/drawings pages, since they do exactly the same (display a gallery, only the folder used to draw images from is different). I was planning on writing 5 controllers: one for each tpl file etc etc.

But I've hit a little snag: With Smarty, the links for the navigation are stored in an array which I loop through to create the nav
Code:
// $pages = array( 'controller' => 'to be displayed in nav', etc, etc)
// $page = $_GET['page'];
<ul>
&lt;?php foreach($pages as $c => $p): ?&gt;
<li><a href="&lt;?=$c?&gt;"&lt;?php if($c == $page) echo ' id="active"'; ?&gt;>&lt;?=$p?&gt;</a></li>
&lt;?php endforeach; ?&gt;
</ul>

Now I realise that for a simple navigation as this, I could just as easily hardcode the links in my view_navigation.php file and leave it at that, but what if I have to dynamically generate that array from, say, a database? My navigation stays the same for the entire site. It seems so silly then to copy paste the build array code in every controller. And what seems even more stupid is that, if I change one line for it, I have to copy paste it in every controller again.

So basically, what I needed to know was: is there a way to execute a certain piece of code for every controller, so that that piece of code can pass along the $pages array, so I can pass the $pages array to my view...

Which leads me to a second question I just thought off: I have to load 3 view for every controller: header, a variable one and footer. Is there a way to automatically load the footer and header view? Purely for the sake of reducing lines of code?

And a third question: how do I check what page I'm currently on? So I can display the proper active state in my menu?


Messages In This Thread
Coding approach question: how to (easily) build a navigation. - by El Forum - 05-28-2008, 03:00 PM



Theme © iAndrew 2016 - Forum software by © MyBB