Welcome Guest, Not a member yet? Register   Sign In
Multi level menu from database (parent->child)
#1

[eluser]JimmyJ[/eluser]
Awe, I don't know how long I've been at this now. If someone can tell me what I'm doing wrong I'll bake some cookies.

Anyway, I have a database
Code:
--
-- Table structure for table `content_menus`
--

DROP TABLE IF EXISTS `content_menus`;
CREATE TABLE IF NOT EXISTS `content_menus` (
  `menu_id` int(11) NOT NULL AUTO_INCREMENT,
  `content_id` int(11) NOT NULL,
  `content_menu_title` varchar(255) NOT NULL,
  `menu_position` bigint(20) NOT NULL,
  `parent` int(11) NOT NULL,
  PRIMARY KEY (`menu_id`),
  UNIQUE KEY `content_menu_id` (`menu_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=51 ;

--
-- Dumping data for table `content_menus`
--

INSERT INTO `content_menus` (`menu_id`, `content_id`, `content_menu_title`, `menu_position`, `parent`) VALUES
(3, 1, 'Home', 0, 1),
(45, 2, 'Services', 1, 1),
(5, 3, 'Clients', 2, 1),
(6, 4, 'Portfolio', 3, 1),
(7, 5, 'Blog', 4, 1),
(8, 14, 'Web Design', 1, 2),
(9, 15, 'Web Design Scotland', 0, 8),
(2, 0, 'Footer Menu', 2, 0),
(12, 6, 'Contact', 5, 1),
(31, 38, 'Web Design', 0, 4),
(1, 0, 'Main Menu', 1, 0),
(43, 39, 'Web Development', 1, 45),
(38, 42, 'Internet Marketing', 3, 45),
(40, 43, 'Web Design', 0, 45),
(46, 44, 'SEO', 2, 45),
(50, 49, 'HTML', 0, 40),
(47, 45, 'Web Design Wales', 1, 8);

And I have a Menu_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->join('content', 'content.id = content_menus.content_id', 'left');
        $q=$this->db->get('content_menus', 'content');
        foreach($q->result() as $r){
            $data[$r->parent][] = $r->parent;
        }
        $menu=$this->build_menu($data);
        return $menu;
    }
    
    
    function build_menu($parent){
        static $i = 1;
        if (isset($parent)) {
            $menu = '<ul>';
            $i++;
            foreach ($parent as $r) {
                $child = $this->build_menu($r->menu_id);
                $menu .= '<li>';
                $menu .= '<a >url_key.'">'.$r->content_menu_title.'link</a>';
                if ($child) {
                    $i--;
                    $menu .= $child;
                }
                $menu .= '</li>';
            }
            $menu .= '</ul>';
            return $menu;
        } else {
            return false;
        }
    }


}

And a controller
Code:
function index()
    {
        
        
        $template = 'page';
        
        $main_menu = $this->menu_model->get_menu_structure();
        $data['main_menu'] = $main_menu;
        
        print_r ($main_menu); die();
        
        $data['content'] = '' . $template . '';
        //$this->output->cache(60);    
        $this->load->view('templates/master', $data);
        
        
        
    }

And a view with

Code:
&lt;?php echo $main_menu; ?&gt;


I'm getting the error:

Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: models/menu_model.php

Line Number: 25

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: models/menu_model.php

Line Number: 27

etc etc

Can anyone tell me what I'm doing wrong? All I need is a simple recursive menu Sad


Messages In This Thread
Multi level menu from database (parent->child) - by El Forum - 02-17-2011, 10:36 AM
Multi level menu from database (parent->child) - by El Forum - 02-17-2011, 12:14 PM
Multi level menu from database (parent->child) - by El Forum - 02-17-2011, 01:28 PM
Multi level menu from database (parent->child) - by El Forum - 02-17-2011, 01:30 PM
Multi level menu from database (parent->child) - by El Forum - 02-17-2011, 02:00 PM
Multi level menu from database (parent->child) - by El Forum - 02-17-2011, 02:37 PM
Multi level menu from database (parent->child) - by El Forum - 02-18-2011, 12:54 PM



Theme © iAndrew 2016 - Forum software by © MyBB