Skip to content
Advertisement

create dynamic menu based on level with codeigniter

I want to make dynamic menu based on laravel. here’s the important information :

  1. table_menu :
    • contains field name etc. and level_id with varchar type
  2. table_level :
    • contains id with int type and name of the menu
  3. I use codeigniter and sql

so the idea is, instead of inserting data to table_menu one by one (for example if I’ve 5 menu and 2 level, that would be 10 menu data in total on table menu).

so I think, the best idea for this is that I just need to insert csv on field level_id in table_menu (level_id => 1(admin),4(user),5(etc). and that will give me 5 menu no matter how much level. right?

But the question HOW? is that even POSSIBLE?

I’ve tried like this :

$this->db->where('parent', true)->where_in('level_id', $this->session->userdata('level_id'))->get('table_menu')->result();

but It didn’t work. can anyone help me?

Advertisement

Answer

finally i figured it out! I read a lot about find_in_set sql query, and finally succeed. here’s the final code :

$this->db->query("SELECT * FROM table_menu where FIND_IN_SET( ".$this->session->userdata('level_id').", level_id) > 0")->result();
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement