Mecom
Mecom

Reputation: 411

Understanding category sub categories and how it works

(Working on mysql)

I have this category-subcategory table (The adjacency model)

CREATE TABLE `categories` (
  `id` int(11) NOT NULL,
  `name` varchar(50) DEFAULT NULL,
  `parentid` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Which works good for up to the first level.

Example:

Computer > Software
Computer > Monitor
Computer > Printer

But what if the the sub-category to is to be divided into more sub-categories to make more sense or easy naviagtion like

enter image description here

My question:

  1. What is further division of a sub-category called? Sub sub category, sub category to nth, I ask because I do not know what to search google for when it comes to sub category further having more categories.

  2. How do you solve this problem, need for further sub sub category? I read somewhere that after the sub-category, which in this case is the monitor, software, printer you use product tags for further division ... this confused me, If this is how it is to be done can you show me lil example.

  3. What exactly is the right way.

Upvotes: 2

Views: 580

Answers (1)

Jamal Abdul Nasir
Jamal Abdul Nasir

Reputation: 2667

I designed the same DB structure for categories in my classifieds portal. And I achieved the nth level of subcategories in one table using the following design.

CATEGORIES TABLE

id | category_title | parent_id
1  | fruit          | null
2  | orange         | 1
3  | apple          | 1
4  | orange-child   | 2

The parent id will be null for parent categories and will have ID of a parent category if it is a child category. The orange is parent and child at the same time. Because, orange is the child of the fruit and parent of the orange-child category. This way you can get the hierarchical structure of categories.

Upvotes: 1

Related Questions