CodeIgniter Forums

Full Version: Building a menu path like a folder structure
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]JimmyJ[/eluser]
Phew, ok got a recursive menu working, but I'm trying to create the paths to the menus look like a folder structure. i.e. Services -> Web Design -> Html would follow the path "http://site.dev/services/web-design/html".

Right now I can't seem to get it past site.dev/html

Does anyone have any examples of how this is done, or can help with the code a little?

Model:

Code:
<?php


class Menu_model extends CI_Model {
    
    function get_menu_structure(){
        //$this->db->where('parent',$parent);
        $this->db->order_by('menu_position','asc');
        $this->db->select('*')->from('content_menus', 'content');
        $this->db->join('content', 'content.id = content_menus.content_id', 'left');
        $q=$this->db->get();
        foreach($q->result() as $r){
            
            $data[$r->parent][] = $r;
        }
        $menu=$this->build_menu($data, 0); // From Parent ID 1
        return $menu;
    }
    
    
    function build_menu($category, $parent){
        static $i = 1;
        //$path = '';
        if (array_key_exists($parent, $category)) {
            $menu = "\n" . '<ul>'."\n";
            $i++;
            foreach ($category[$parent] as $r) {
                $child = $this->build_menu($category, $r->menu_id);
                //$path .= '' . $r->url_key;
                $menu .= "\t" . '<li id="menu-' . $r->menu_id . '">';
                $menu .= '<a >url_key . '">'. $r->content_menu_title . '</a>';
                if ($child) {
                    $i--;
                    $menu .= $child;
                }
                $menu .= '</li>'."\n";
            }
            $menu .= '</ul>'."\n";
            return $menu;
        } else {
            return false;
        }
    }


}

Which produces:

Code:
<ul>
    <li id="menu-3"><a href="http://site.dev/">Home</a></li>
    <li id="menu-45"><a href="http://site.dev/services">Services</a>
        <ul>
            <li id="menu-40"><a href="http://site.dev/web-design">Web Design</a>
             <ul>
                 <li id="menu-50"><a href="http://site.dev/html">HTML</a></li>
             </ul>
       </li>
       <li id="menu-43"><a href="http://site.dev/web-development">Web Development</a></li>
       <li id="menu-46"><a href="http://site.dev/seo">SEO</a></li>
       <li id="menu-38"><a href="http://site.dev/internet-marketing">Internet Marketing</a>    </li>
</ul>
</li>
    <li id="menu-5"><a href="http://site.dev/clients2">Clients</a></li>
    <li id="menu-6"><a href="http://site.dev/portfolio">Portfolio</a></li>
    <li id="menu-7"><a href="http://site.dev/blog">Blog</a></li>
    <li id="menu-12"><a href="http://site.dev/contact">Contact</a></li>
</ul>

El Forum

[eluser]JimmyJ[/eluser]
Got it Smile

If anyone's interested, the full Menu_Model is:
Code:
&lt;?php


class Menu_model extends CI_Model {
    
    function get_menu_structure(){
        //$this->db->where('parent',$parent);
        $this->db->order_by('menu_position','asc');
        $this->db->select('*')->from('content_menus');
        //$this->db->join('content', 'content.id = content_menus.content_id', 'left');
        $q=$this->db->get();
        foreach($q->result() as $r){
            
            $data[$r->parent][] = $r;
        }
        $menu=$this->build_menu($data, 1); // From Parent ID 1
        return $menu;
    }
    
    
    function build_menu($category, $parent){
        static $i = 1;
        if (array_key_exists($parent, $category)) {
            $menu = '<ul>';
            $i++;
            foreach ($category[$parent] as $r) {
                $child = $this->build_menu($category, $r->menu_id);
                $menu .= '<li class="' . $r->menu_url_key . '">';
                
                $dir_path = '';
                $path=$this->get_path($r->parent);
                foreach($path as $url) {
                
                $dir_path .= $url;
                
                }
                
                $menu .= '<a >menu_url_key . '">';
                $menu.= $r->content_menu_title . '</a>';
                
                if ($child) {
                    $i--;
                    $menu .= $child;
                }
                $menu .= '</li>';
            }
            $menu .= '</ul>';
            return $menu;
        } else {
            return false;
        }
    }
    
    function get_path($parent) {
    
       $result = mysql_query('SELECT * FROM content_menus '.'WHERE menu_id="'.$parent.'";');
       $r = mysql_fetch_array($result);
    
       $path = array();
    
       if ($r['parent']!='') {
          
           if ($r['menu_url_key'] == '') {
           $path[] = $r['menu_url_key'];
           } else {
           $path[] = $r['menu_url_key'] . '/';    
           }
    
           $path = array_merge($this->get_path($r['parent']), $path);
       }
        
       return $path;
    }


}

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

class Site extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        
        
    }

    function index()
    {
        
        
        // Default template
        $template = 'page';    
        
        
        $main_menu = $this->menu_model->get_menu_structure(1);
        $footer_menu = $this->menu_model->get_menu_structure(2);
        $data['main_menu'] = $main_menu;
        $data['footer_menu'] = $footer_menu;
        
        print_r ($main_menu); print_r ($footer_menu); die();
        
        $data['content'] = '' . $template . '';
        //$this->output->cache(60);    
        $this->load->view('templates/master', $data);
        
        
        
    }
    
    
}

/* End of file site.php */
/* Location: ./application/controllers/site.php */