Welcome Guest, Not a member yet? Register   Sign In
Navigation for all Controllers
#1

[eluser]sufijen[/eluser]
Hi,

I'm pretty new with CI and tried to find the result for my question in this forum. But as you see i didn't found it yet Wink
I want a Navigation for my Blog. And i don't want to call it in each Controller.
I have searched the forum for 2 days now.
At the moment i know that i can do it with a Model and a Hook.
But i have the Problem that i need to get information from Database.

So i set up a Hook(config):
Code:
$hook['post_controller'] = array(
                                'class'    => 'Navigation',
                                'function' => 'getCategories',
                                'filename' => 'Navigation.php',
                                'filepath' => 'hooks',
                                );

The Hook itself:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Navigation
{
    var $CI;
    
    function __construct()
    {
        $this->CI = get_instance();
    }
    
    function getCategories()
    {
        $assign['categories'] = $this->db->get('categories');
        
        $this->CI->load->view('main', $assign);
    }
}

?>

And there is the problem. I don't know how to set or load the view.

In Html (Main Template) it looks like this:
Code:
<ul>
            &lt;? foreach ($categories->result() as $category): ?&gt;
            <li>&lt;?=anchor('category/'.$category->url_title, $category->title)?&gt;</li>
            &lt;? endforeach; ?&gt;
        </ul>

But i have real problems to reach what i want.

Can you give me a Result or some hints?

Greez Sufijen
#2

[eluser]xwero[/eluser]
the best way is to use a template library where you can define pageparts so you won't have to worry about reused parts.
#3

[eluser]Rick Jolly[/eluser]
Have you read the FAQ?
http://codeigniter.com/wiki/FAQ/

There are tons of ways to include a common partial view within some or all views. There are two components to every dynamic view/partial:
1) the view file
2) the data

You can load the data with the partial view:
$partial = $this->load->view('partial_view_file', $data, true);

Or you can load the data for the view seperately:
$this->load->vars($data_for_partial_view);
$partial = $this->load->view('partial_view_file', '', true);

Now to try to answer your question...

One way to accomplish your goal would be to load the data for the partial view within the constructor of a parent/base/application controller from which all your other controllers extend. That takes care of the partial view's data. Now to load the partial view, you can simply do that in your main layout view template(s):
...some html...
$this->load->view('partial_view_file');
...more html...

EDIT:
Another method is similar to what is mentioned above. But instead of loading the data in the base/parent/application controller and the partial view within the main layout view, you can load the data and partial view together into a variable that can be referenced in all main layout views:
$data['partial'] = $this->load->view('partial_view_file', $data_for_partial_view, true);
$this->load->vars($data);
then in your main layout view(s):
...some html...
&lt;?php echo($partial); ?&gt;
...more html...

If you don't want to touch the controller at all, you can use a helper in the main layout view that loads the data and the partial view template at the same time - all within the view. That's my favorite approach. Just do a search for hmvc on these forums.
#4

[eluser]sufijen[/eluser]
Thank you i'll try to find the result.

Thanks for these hints
#5

[eluser]esra[/eluser]
There is a wiki article called nested sets and a seaprate forum thread by Thunder UK. The included sample application is called categories. Nested sets is based on Joe Celko's nested sets model for making a relational database support hierarchies. It is possible to modify the sample application to create either a toolbar or menu for loading MVC agents (MVC triads composed of a controller, model and view).
#6

[eluser]sufijen[/eluser]
I got it:

I made one libary and that's it Wink

I did it like this:

Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Navigation {
    
    var $ci;
    
    function __construct()
    {        
        $this->ci =& get_instance();
        $this->loadCategories();
    }
    
    function loadCategories()
    {
        $data['categories'] = $this->ci->db->get('categories');
        
        $this->ci->load->view('main', $data, true);
    }
    
    function loadAdminNavi()
    {
        
    }
    
    
}

?&gt;

And than i autoloaded the Library




Theme © iAndrew 2016 - Forum software by © MyBB