I'm currently developing a custom shopping cart/site management system for a client and I ran into an interesting dilema - I thought I'd share and see what ideas others had on this topic.
The dilema is in creating a category system over multiple sites where there's no limit to how many sub-categories there are (ie: a tree with ever expanding branches). So, I came up with this solution:
CREATE TABLE categories (
categoryId smallint(6) NOT NULL auto_increment,
category tinytext NOT NULL,
visible tinyint(1) NOT NULL default '0',
PRIMARY KEY (categoryId)
) TYPE=MyISAM;
CREATE TABLE sites (
siteId tinyint(4) NOT NULL auto_increment,
url tinytext NOT NULL,
status tinytext NOT NULL,
PRIMARY KEY (siteId)
) TYPE=MyISAM;
CREATE TABLE categories_to_sites (
relSiteId tinyint(4) NOT NULL default '0',
relCategoryId smallint(6) NOT NULL default '0',
KEY relSiteId (relSiteId,relCategoryId)
) TYPE=MyISAM;
CREATE TABLE category_to_category (
relCategoryId smallint(6) NOT NULL default '0',
relSubCategoryId smallint(6) NOT NULL default '0',
KEY relCategoryId (relCategoryId,relSubCategoryId)
) TYPE=MyISAM;
*note relSubCategoryId actually relates to a category id but can't have 2 relCategoryId in the same table
So, this is all well and great going in, you add site(s), you add categories, you relate which site(s) the categories are visible on, and you relate which category is in which category. The interesting part crops up bringing it back out of the database.
Say you need to display something like this:
Category1 >> Category 17 >> Category 58 >> Category 129
In my mind, the only way to display that would be to do a SELECT for each category.
Can anyone else see another way? I'd be interested to know how others have designed category structures.
JMJimmy