CodeIgniter Forums
Navigation with links from db. How? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Navigation with links from db. How? (/thread-7572.html)



Navigation with links from db. How? - El Forum - 04-15-2008

[eluser]halex[/eluser]
I want to make navigation with link from a table, say 'links'.
A bad solution:
Any controller:
Code:
...
$data['for_left_nav']=$this->Links_model->get_all();
$data['for_content']=...
$this->load->view('partials/header', $data);
$this->load->view('view');
$this->load->view('partials/footer');

In ...partial/header:
Code:
<div id="container">
<div id="left_nav">
  <ul>
  &lt;?=foreach($for_left_nav as $link: ?&gt;
  <li>&lt;?=anchor($link,...</li>
  </ul>
....
</div> //left_nav
<div id="content">

This way I make database access for each page which use left_nav, although changes in 'links' table seldom happens.
Another solution is to search in sources as wordpress or joomla, but... I'm lazy ;-)
Are anybody a better solution for that?
Thanks.
(As you can see, I'm not an English speaker/writer, sorry)


Navigation with links from db. How? - El Forum - 04-17-2008

[eluser]halex[/eluser]
I've searched again and it seems that isn't another solution.
I'm a disappointed n00b!


Navigation with links from db. How? - El Forum - 04-17-2008

[eluser]nmweb[/eluser]
You can use a file cache or any other cache (memory) to store the links. This way you don't have to query the db on every request. I also recommend putting this code in a Wiredesignz hmvc module.


Navigation with links from db. How? - El Forum - 04-17-2008

[eluser]halex[/eluser]
Thanks, nmweb!
I haven't used caching yet, is a good tip.
Thanks again.


Navigation with links from db. How? - El Forum - 04-17-2008

[eluser]deck1[/eluser]
You can use something like this:

In your controller:

Code:
$this->load->library('layout');

//instead of this->load->view
$this->layout->display('your_main_content_view', $data);

application/libraries/Layout.php:
Code:
&lt;?php  

class Layout
{

    var $CI;

    function Layout()
    {
        $this->CI =& get_instance();
    }

    function display($view, $data=NULL)
    {
        //Header with menu from DB
        $data_header['top_menu'] = 'Menu from DB...';
        $this->CI->load->view('header_view', $data_header);
        
        //Main content
        $this->CI->load->view($view, $data);
        
        //Footer
        $data_footer['bottom_menu'] = 'Menu bottom';
        $this->CI->load->view('footer_view', $data_footer);
    }
}
?&gt;



Navigation with links from db. How? - El Forum - 04-21-2008

[eluser]Chris Newton[/eluser]
That assumes you're already using the layout library, which isn't part of the default install: http://codeigniter.com/wiki/layout_library/


You could also write out the navigation to a view file when the links table is updated. Long-term caching. Basically you pull the navigation from a file, rather than the database. If you're making changes to the 'links' table through an admin interface, that admin application could also write out a new view_navigation.php file at the same time the database is being updated.