I need advices for a table structure.
I need to create menus dynamically.
There will be more than 3 menus.
Some of menus will have multi-level structures.
What is the best way to strucure the menu tables?
All the menus are in one table
Each table should have one table
Any other??
Please give me your experience or common practices for dynamically generated menus please.
assuming your menu tree is not very large, the so-called adjacency list would be the simplest option
id
parent_id
title
other fields
for top level menus parent_id = 0
see Managing Hierarchical Data in MySQL for further discussion and other options
You could use a structure like this:
table name: menu
id - primary key
menu_id - reference to the parent menu item
title - title of the menu
(any other data you need)
this way you can have unlimited levels ...
off course this is not the only way ...
Using parent_id should be enough for most structures, however if you get stuck on performance (with regards to deep recursion), you should also research Modified Preorder Tree Traversal.
http://www.codeproject.com/Tips/354696/Dynamically-populating-menu-items-from-the-databas
this will be helpful,
Dynamically populating menu items from the database in ASP.NET
Related
I would like to store a nested associative array in MySQL. I need this for a nestable navigation menu on my site. There should be no limit to the level of nesting.
I have researched the nested set model and the adjacency list, but am unsure of which to use for my needs.
Ideally, I will be able to query my table in a way that I can reconstruct my associative array in PHP and then use this to construct my navigation menu.
Records may be added / changed / re-ordered, but this will only happen infrequently.
I am looking for an example table structure for MySQL (InnoDB), queries to get and re-order the records, and update, delete and add new records. I am using PDO in PHP so any example code of how to turn the record set into the associative array would also be helpful.
Try the following design:
Column 'BEFORE'
Column 'AFTER'
Column 'NAME'
Column 'ID'
Now, just store ID as required.
Example: The navigation menu might be
A --> B --> C
Just give an ID to each entry in the menu and store it with an unique ID.
Then enter the BEFORE and AFTER values as IDs into the table.
So, for B we have
Entry for B:
BEFORE = ID(C)
AFTER = ID(A)
NAME = 'B'
This can be easily expanded for multiple nested menus.
I would suggest following design for implementing the solution.
Table : Navigation
Columns :
ID (INT NOT NULL IDENTITY),
Description (VARCHAR),
ParentID (INT NULL) - Refer its own table ID column,
AdditionalColulmns..
I stucked in a Problem When Playing with Drop Downs.I tried to use this
http://www.grocerycrud.com/forums/topic/1087-updated-24112012-dependent-dropdown-library/
But actually my requirement is quite different. I have a table fwld_products in which i am adding all other table's categories, from
fwld_cat_main (main Category's ID),
fwld_cat_sub1 (sub1 Category's id)
fwld_cat_sub2 (sub2 Category's id)
fwld_cat_sub3 (sub3 Category's id)
I want to Display Dropdown in such a way, when user Selects main
Category, the Drop Down Appear (sub1) Having Data related to main
category and when sub1 selected drop down appear (sub2) showing data
related to sub1, and sub2 selected and drop down appear(sub3) to show
data related to Drop down (sub2).
When submitted Finnally data inserted to [fwld_products].
Here I am attaching ERD, and result as well.
Please help
Hold on, it seems that DB structure of your categories table needs to be improved. What I suggest is that you follow footprints from some of the popular CMS like Opencart. It will give you a great sense to accomplish your task. You can easily optimize your DB by using just one "category" table (instead of main, sub1, sub2, and sub3 category tables) like this:
For category names, description, and meta keywords etc you can create this table "category_description":
Finally to assign categories to the products you can simply create another table "product_to_category":
In this way you can easily manage your data in DB and you can now easily tackle your situation using Codeigniter and Grocerycrud.
Try, Chained Selects Plugin for jQuery and Zepto (Github Project | Project Home)
If you use jquery or zepto in your project, this plugin will help you to solve your problem, Specially the remote version. You can create related select boxes easily.
Hope this helps :)
Let's say I have a bunch of businesses. And each business can have multiple categories, subcategories and sub-subcategories (three levels). Let's say I set up a table according to the nested set model for my categories.
How do I now use this table and assign categories to each business? I understand I will need another table but what node gets assigned? Is it the lowest level node?
business_id category_id
And then what's the right way to retrieve all the categories for each business?
The way this generally works is that you assign the leaf or lowest-level-node. Then when you are querying to get the full hierarchy you traverse up the tree to the root. It is generally much easier (especially in MySQL) to traverse from leaf to root then vice versa.
Here is the best link I've found that describes how to accomplish this query for a tree of dynamic size (the link you've included assumes that the tree is always 3 levels deep)
I have a database of 30000 products and all products has assigned different categories. There is a separate table for all categories & sub-categories are also stored into same table.
The table structure is something like this
Category_mst
category_id_pk, category_name, parent_id
Product_mst
product_id_pk, product_name, category_id_fk (reference to Category_mst->category_id_pk)
Categories are stored upto any number of sub levels.
So there can be categories like below
Clothes > Shirts > Kids > Red Color
Products are stored on any level of Category.
I want to make a query or a php script that can find out all the Categories that have no Products under it(upto any sub level).
How can I do this ?
Thanks in advance.
You are trying to store hierarchical data in a database.
The naive approach to this is a very poor one, because MySQL does not support recursive queries (unlike Oracle or SQL-server).
Change your database design.
See: The Nested Set Model hierarchical data
Implementing a hierarchical data structure in a database
I have a tree of categories in my database.
I also have a table of items associated with the tree by a category id.
Now, I want to list all items in a specific category and its children and their children, etc...
For now, I proceed this way:
Retrieve the id of all concerned categories.
Make a query in the items table with a WHERE clause like this: WHERE cat_id=2 OR cat_id=10 OR ...
I think this way cause the query to be very slow and very long if I have a lot of categories. A search can be in 100 categories sometimes.
Is there a better practice?
From gugl on "storing tree in relational database": http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
Adjacency List is simple, but not good in most complex cases
Nested Set is complex from 1st view (mostly during write), but it much more like standard for storing and reading trees in RDBMs.
+1 about
EXPLAIN select * from table
that will help you to see bottlenecks.
Also try instead of
column1 = 1 or column1 = 2
something like:
column1 in (1, 2)
But anyway without indexes it wouldn`t help.