Welcome Guest, Not a member yet? Register   Sign In
Please help me coding static page
#9

[eluser]Josh Bright[/eluser]
Hmm, if what your looking for is a way to have a few different controllers (each with their own methods/pages), and have a menu where you can navigate between all these things, I would just use a view.

Its actually much easier if you look into building a template system for Codeigniter. By doing this, you can really easily add a menu view to your site and have it on all your pages.

Here is how I accomplish this:

Example Controller
Code:
class Test_Controller extends Controller {
     public function index() {
          $data['main_content'] = "index_view";  //This will be my view file that gets loaded
          $this->load->view('template/template_view', $data); //My template view (more below)
     }
}

So, then in my views folder I have a template folder. In that folder is a few files, one of them being this template_view file. Here is a mockup of that file:

Code:
$this->load->view('template/html_header_view');
    
    if (isset($page_header)) {
        $this->load->view($page_header);
    }
    
    if (isset($navigation_content)) {
        $this->load->view($navigation_content);
    }
    
    $this->load->view($main_content);
    
    $this->load->view('template/footer_view');

So, as you can see here, the view I call, first calls a html_header_view file. This is pretty much everything from the start of the page, up to the opening body statement. After that I check to see if I have passed in a page_header view, which would be like the top part of the page with the website name and such.

I then check if I passed any data into a $navigation_content variable. If I did, I'll load that view file up next. This is where my menu resides.

I'll then load the view I passed as $main_content (that index_view file), and finally a footer_view file that wraps up my body tag and anything else I might want there.

Now, finally, my menu view looks something like:
Code:
<?php
    $menu = array(
        'Home'=>'main/index',
        'Upload File'=>'upload/index',
        'History'=>'history/index',
        'Preferences'=>'prefs/index',
        'Logout'=>'logout/index'
    );
    
    $uri_segment = $this->uri->segment('1');
    if ($uri_segment == "") {
        $uri_segment = "main";
    }
?>

<div id='site_menu'>
    <ul>
        &lt;?php
            foreach ($menu as $key=>$item) {

                $uri_parts = explode("/", $item);
                if ($uri_segment == $uri_parts['0']) {
                    echo "<li class='menu_selected'>";
                } else {
                    echo "<li class='menu_not_selected'>";
                }
                echo anchor($item, $key);
                echo "</li>";
                $uri_parts = "";
            }
        ?&gt;
    </ul>
</div>


So, you can see that I first have a $menu array. This a pretty simple way of doing this, but, it works just fine. The key to each item in the array is what would show up on the screen for the menu item, and the value is the url that will get put into the <a> tag.

After that menu array I get a variable setup to show my current controller im in (using that $this->uri->segment() method). If someone just goes right to my site without clicking any links, the default controller might not be shown, so, if the $uri_segment is empty I just tell it that its 'main' (my default controller from routes.php)

Below that I build the actual menu using that $menu array. I'll loop through the menu array, and the first thing I do is explode the url string, and grab the controller name from it. If the controller name from the array matches my current controller name, I wrap the <a> tag in an <li> tag with a class of menu_selected, if its not, the <li> tag has a menu_not_selected class.

So, there's really nothing that you need to do with routing, besides setting your default controller. There's really nothing that 'links' these controllers together or anything, just by building a list of links that point to your various controllers you'll end up with, I think, what your looking for.

Lemme know if im way off base or if this helps, or if you want me to explain something a bit more in depth.

Josh B


Messages In This Thread
Please help me coding static page - by El Forum - 06-29-2010, 12:36 AM
Please help me coding static page - by El Forum - 06-29-2010, 04:18 AM
Please help me coding static page - by El Forum - 06-29-2010, 06:34 AM
Please help me coding static page - by El Forum - 06-29-2010, 07:00 AM
Please help me coding static page - by El Forum - 06-29-2010, 11:33 AM
Please help me coding static page - by El Forum - 06-29-2010, 12:00 PM
Please help me coding static page - by El Forum - 06-29-2010, 12:04 PM
Please help me coding static page - by El Forum - 06-29-2010, 12:58 PM
Please help me coding static page - by El Forum - 07-01-2010, 04:49 PM



Theme © iAndrew 2016 - Forum software by © MyBB