Say we have a csv file : Is there a php solution to dynamically convert the category string into mysql nested categories and subcategories ? something like categories table : id | cate | parent_id 1 | toys | 0 2 | baby toys | 1 3 | Baby Rattles | 2 and so on Further categories are first checked if they exist , and if they do just return their id. I have implemented this solution before , but it was a bunch of messy copy paste code. Wondering if there is an existing script or a known method to code this ? Thanks
Grab the WordPress plugin "Batch category import" - even if you don't use WordPress. It's written in php and it takes a string like that (though it's category->subcat) and processes it into WP categories and subcats. You should be able to decipher the code and redo it a bit to do what you want, though you might have a stumble over the WP terms. It'll be a good starting point, at least.
If you run into significant problems, pop me a PM or contact me through my site (signature). I'm no pro, but I know WP/SQL fairly well. It's obviously better if you can produce your own (especially for maintenance!) but I'm always happy to try to lend a hand.
Preferrably, you'd create several tables for this in the database - that's what relational databases is for. Table 1: categories - id, name Table 2: sub_categories - parent_id (from categories table), id, name Table 3 to infinity: name_of_final_category - parent_id (from sub_categories table), id, name
^I was thinking of following the adjacency list model. Besides wouldn't dividing into several tables won't be expensive when trying to retrieve the full tree ? THANKS that is very generous of you
No. Not really. Normally, you wouldn't want to get the whole three, either. You'll pull the main categories, and if one of them are clicked, pull the sub-categories, and so forth. However, pulling all categories won't be that much more expensive, it's just a matter of joining a few tables together.