I am currently building a store based module for one of my customers. The items have many different types of categories that they can be entered into. An example I will use is a radio.
It can be a handheld or mobile type of radio. We have the radios listed under the following path:
store -> radios -> brand -> mobile / handheld -> item page
This is pretty simple to store if that was the same layout for everything, but alas it is not that easy. They may have an accessory that works for all the radios and brands, in which case it would be stored under like this:
store -> radios -> accessory -> item page
This is a complete category below the radios so writing the select would have to be different for every item group which is less than desirable. The items also should be accessible from the shared parent category, like if I were to visit store -> radios
I should see all of the items under those sub categories. Here is the current database structure I have planned, which is where I am having the most issues I believe (P.S: the ID category is unique, primary, and auto increment):
+===================================================+
| ID | Category Name | Path | Parent ID |
+===================================================+
| 1 | radios | radios | 0 |
| 2 | accessory | misc | 1 |
| 3 | motorola | motorola | 1 |
| 4 | handheld | handheld | 3 |
| 5 | mobile | mobile | 3 |
+===================================================+
I then set the category_id
on the product to say.. 5 for my mobile radio that I add to the products page. If I visit the product page, I need to get the path for each of the parent categories to display on the page, such as
store/radios/motorola/mobile/product-page-here.html
I would have to build that path from the above database using the path field and joining for ALL of the parent classes to get a correct path. Right now, I am doing many select * FROM tblname WHERE id = parent
and then in the while loop of that command, doing another select the same way until I get back to the parent table. There has to be a better way to do that, especially since if I only went 3 levels deep, and the customer adds a sub category that is 4 levels deep, it would never be in the right path.
The final problem is, using the example above, if I instead went to store/radios/motorola
, that would try and fetch any items listed under the category_id
for 3
instead of 5
, and the item would not be shown under the general tab of motorola
since it's instead a child of the mobile
table, an indirect child of the motorola
table. By giving them more than 1 sub category option, its made planning this whole system a royal pain.
Anyways, thanks for reading, leave your comments or suggestions below. Btw, this is all being stored in MySQL and called from a PHP script.