CodeIgniter Forums

Full Version: list menu and submenu from two table
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I am new in codeigniter and I need help off course for one problem.

I have two tables, menu and submenu.

Table menu:
id_menu, menu_name
1, Menu 1
2, Menu 2
3, Menu 3

Table submenu;
id_sub, id_menu, sub_name
1,1, Sub 1.1
2,1, Sub 1.2
3,1, Sub 1.3
4,2, Sub 2.1
5,3, Sub 3.1
6,3, Sub 3.2

How I can using MVC get in view next list:

Menu 1
 Sub 1.1
 Sub 1.2
 Sub 1.3
Menu 2
 Sub 2.1
Menu 3
 Sub 3.1
 Sub 3.2

Thanks in advance
You're probably looking for more specifics, but, as a start:
- Build a model which uses query builder or db->query() to retrieve the list from the tables. Depending on your comfort level and whether you already have the SQL (or a stored procedure) to retrieve the list, you have to make some trade-offs between the number of queries to the database (or the complexity of the query/queries) and the amount of work you'll do in PHP to build the list. The fact that the data is in two tables (rather than one) tells me that someone may have had a specific query in mind, but I could be wrong.
- Your controller would load the model and call the method to retrieve the list, then load a view, passing the list to the view

If you're going to use this in multiple places on your site, you may want to use a library to load the model, retrieve the data, and load the data into a partial view (or return the data), so you can load the library to get the menu in whatever controller needs to do so.
(10-21-2015, 12:55 PM)mwhitney Wrote: [ -> ]You're probably looking for more specifics, but, as a start:
- Build a model which uses query builder or db->query() to retrieve the list from the tables. Depending on your comfort level and whether you already have the SQL (or a stored procedure) to retrieve the list, you have to make some trade-offs between the number of queries to the database (or the complexity of the query/queries) and the amount of work you'll do in PHP to build the list. The fact that the data is in two tables (rather than one) tells me that someone may have had a specific query in mind, but I could be wrong.
- Your controller would load the model and call the method to retrieve the list, then load a view, passing the list to the view

If you're going to use this in multiple places on your site, you may want to use a library to load the model, retrieve the data, and load the data into a partial view (or return the data), so you can load the library to get the menu in whatever controller needs to do so.

How to write model to list all data from one table and for each row to list data from another table related for that id_menu?
(10-24-2015, 04:42 AM)vladakg Wrote: [ -> ]
(10-21-2015, 12:55 PM)mwhitney Wrote: [ -> ]You're probably looking for more specifics, but, as a start:
- Build a model which uses query builder or db->query() to retrieve the list from the tables. Depending on your comfort level and whether you already have the SQL (or a stored procedure) to retrieve the list, you have to make some trade-offs between the number of queries to the database (or the complexity of the query/queries) and the amount of work you'll do in PHP to build the list. The fact that the data is in two tables (rather than one) tells me that someone may have had a specific query in mind, but I could be wrong.
- Your controller would load the model and call the method to retrieve the list, then load a view, passing the list to the view

If you're going to use this in multiple places on your site, you may want to use a library to load the model, retrieve the data, and load the data into a partial view (or return the data), so you can load the library to get the menu in whatever controller needs to do so.

How to write model to list all data from one table and for each row to list data from another table related for that id_menu?


Look at this you will need to make some changes but should point you in the right direction.

Dynamic Multi-level CSS Menu with PHP and MySQL. SEO Ready