Welcome Guest, Not a member yet? Register   Sign In
Navigation menu from database
#1

[eluser]Walle[/eluser]
Hello,

I am new to CodeIgniter, and I have a question which I'm not able to resolve.
I have several views, which load a navigation menu. For example this view:

Code:
<?php $this->load->view('template/header'); ?>
<?php $this->load->view('template/navigation'); ?>
<div id="page-wrapper">
    <div class="row">
        <h1>Title</h1>
    </div>
    <div class="row">
        <p>Paragraph</p>
    </div>
</div>
&lt;?php $this->load->view('template/footer'); ?&gt;

Now my question is, I have some items in the navigation file, which have to be resolved from the database. I have created a model, which returns my data as I want it. I am able to print the data on a view, so that is all right.

Code:
&lt;?php

class Menu extends CI_Model {

    public function getMenuItems() {
        $query = $this->db->query("SELECT ID, OMSCHR FROM ticket.online_help_menu_top");
        $menu = array();
        foreach ($query->result_array() as $row) {
            $menu[] = array(
                'id' => $row['ID'],
                'omschr' => $row['OMSCHR']
            );
        }
        return $menu;
    }
}

I also made a controller to load the view:

Code:
&lt;?php

class Test123 extends CI_Controller {

    public function test() {
        $this->load->model('menu');
        $data = array();
        $data['menu'] = $this->menu->getMenuItems();
        $this->load->view('template/navigation', $data);
    }
}

But if I load the view 'template/navigation', I logically just get a page with my menu on it, and not with all the other stuff like the example on the top of my post.

So my question is, how can I get the data from the Menu model into my template/navigation page, on another view, by just using
Code:
&lt;?php $this->load->view('template/navigation'); ?&gt;
inside that view?

I've been doing some research on the internet, but I wasn't able to find somehting useful on it.

If I need to post some code of the template/navigation.php file, ask it. But it is clearly simple, just a file with <ul> and <li> items, I just need to get the data in there.

I hope someone can answer my question. It would really help me out a lot.

Thanks in advance,

Jeroen
#2

[eluser]mikemiller[/eluser]
You need to pass the data required by the navigation view into the top level view to then pass into the nav view.

Controller code:
Code:
&lt;?php

class Test123 extends CI_Controller {

    public function test() {
        $this->load->model('menu');
        $data = array();
        $data['menu'] = $this->menu->getMenuItems();
        $this->load->view('template/{whatever top level view is called}', $data);
    }
}

View code:
Code:
&lt;?php $this->load->view('template/header'); ?&gt;
&lt;?php $this->load->view('template/navigation',$menu); ?&gt;
<div id="page-wrapper">
    <div class="row">
        <h1>Title</h1>
    </div>
    <div class="row">
        <p>Paragraph</p>
    </div>
</div>
&lt;?php $this->load->view('template/footer'); ?&gt;

Your data array would also need to hold the info for the top level view and anything you need to pass into the template views
#3

[eluser]CroNiX[/eluser]
Instead of loading the navigation view in the main template, load the view as a variable in your main controller and pass it to the main view.

In main template:
Code:
&lt;?php $this->load->view('template/header'); ?&gt;
&lt;?php echo $navigation; ?&gt;
<div id="page-wrapper">
    <div class="row">
        <h1>Title</h1>
    </div>
    <div class="row">
        <p>Paragraph</p>
    </div>
</div>
&lt;?php $this->load->view('template/footer'); ?&gt;

In your controller,
Code:
$this->load->model('menu');
$data = array();
$menu['menu'] = $this->menu->getMenuItems();
//3rd parameter of TRUE returns view instead of outputting it
$data['navigation'] = $this->load->view('template/navigation', $menu, TRUE);
#4

[eluser]CroNiX[/eluser]
mikemiller's answer will also work.
#5

[eluser]InsiteFX[/eluser]
You can also pass the data to any views global by using:
Code:
// make $data global to all views in the main view.
$this->load->vars($data);
$this->load->view('your_main_view');
#6

[eluser]Walle[/eluser]
Thanks, it works now!
#7

[eluser]Unknown[/eluser]
Help need
Please can you give me a Horizontal navigation bar code.
I am a new web designer so give me a simple code so that i can do this easily.
thank you




Theme © iAndrew 2016 - Forum software by © MyBB