mysql – Dynamic menu and Sub Menu in PHP Codeigniter-ThrowExceptions

Exception or error:

I am trying to create dynamic menu using PHP Codeigniter.

There are some Main Menu
Each Main Menu have some Sub_Menu Like, Categories ->Hardware, Software etc.

I somehow made it but there is an issue, the Main menu is showing correctly but the sub menu under main menu is repeating in each Main menu.

This is the image of the issue:

Main Menu 1

Main Menu 2

My View Code

<ul class="slimmenu">
    <?php foreach($menus as $menu): ?>

    <li>
        <a href="#"><?php echo $menu -> Menu_Name;?></a>
        <ul>
        <?php foreach($submenus as $submenu): ?>
            <li>
                <a href="#"><?php echo $submenu -> Sub_Menu_Name;?></a>


            </li>

        <?php endforeach;?>
        </ul>
    </li>
    <?php endforeach;?>

</ul>

My Controller Code

    <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class MainSystem extends CI_Controller
{
    function __construct() {
        parent::__construct();
        $this->load->model('menusys','ms');
    }

    function menu()
    {
        $this->data['menus'] = $this->ms->get_menu();
        $this->data['submenus'] = $this->ms->get_submenu();
        $this->load->view('index', $this->data);
    }

    function add_menu()
    {
        $data = array(
            'Menu_Name' => $this->input->post('mname'),
            'Menu_Link' => $this->input->post('mlink'),

        );
        $this->ms->adm($data);
    }

    function sub_menu()
    {
        $this->data['mmenu'] = $this->ms->get_menu();
        $this->load->view('submenu', $this->data);
    }


    function add_submenu()
    {
        $data = array(
            'Main_Menu_Name' => $this->input->post('mmname'),
            'Sub_Menu_Name' => $this->input->post('mname'),
            'Sub_Menu_Link' => $this->input->post('mlink'),

        );
        $this->ms->adsm($data);
    }

}

?>

My Model Code

<?php
defined('BASEPATH') or exit('no direct script directy allowed');

/**
 *
 */
class menusys extends CI_Model
{

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

    public function adm($data)
    {
        $this->db->set('Is_Active',0);
        $this->db->insert('main_menu',$data);
        $this->session->set_flashdata('msg', 'Meun has been added');
        redirect('MainSystem/menu');
    }


    //sub-menu Section

    public  function get_menu()
    {

        //$this->db->where('Is_Active',1);
//        $this->db->select('*');
//        $this->db->from('main_menu');
//        $this->db->join('sub_menu', 'main_menu.Menu_Name = sub_menu.Main_Menu_Name','inner');
//        $query = $this->db->get();
//        return $query->result();
//
        $query=$this->db->get_where('main_menu',array('Is_Active'=>1));
        return $query->result();

    }
    function get_submenu()
    {
        $this->db->where('main_menu.Menu_Name = sub_menu.Main_Menu_Name');
        $this->db->select('*');
        $this->db->from('sub_menu');
        $this->db->join('main_menu', 'main_menu.Menu_Name = sub_menu.Main_Menu_Name','inner');
        $query = $this->db->get();
        return $query->result();


    }

    public function adsm($data)
    {
        $this->db->set('Is_Active',1);
        $this->db->insert('sub_menu',$data);
        $this->session->set_flashdata('msg', 'Meun has been added');
        redirect('MainSystem/menu');
    }


}

What I Want: I want that each Sub_Menu will show under his parent menu.

Any help will be appreciated.

UPDATE

This is sub-menu table image.

sub-menu table

How to solve:

you need to format your menu array and loop it something like this

$menu = array(
    'menu_name_1' => array(
        'submenu1_1' => 'www.something.com',
        'submenu1_2' => 'www.something.com',
    ),
    'menu_name_2' => array(
        'submenu2_1' => 'www.something.com',
        'submenu2_2' => 'www.something.com',
    )
);

foreach($menu as $menu_name => $submenu){
    echo $menu_name.'<br>';
    if (!empty($submenu)){
        foreach($submenu as $submenu_name => $submenu_link){
            echo '<a href="'.$submenu_link.'">'.$submenu_name.'</a><br>';
        }
    }
}

You have array with menu names and submenu. First you loop menu name and check dose it has submenu, it if has, you loop submenu and make links.

Leave a Reply

Your email address will not be published. Required fields are marked *