Forum Moderators: coopster
I displayed each main category with sub categories underneath using a while loop within a while loop.
maincat
subcat1
subcat2
etc
is there a better way to do such without having to change the database? I ask because I'm not sure how such a structure would hold up to any sort of load.
I'm thinking of eventually outputting it to a file and just including that file instead of pulling from the database every time. Maybe setup a cron to update it regularly or some sort of if check with some date functions. Any tips to steer me in the right direction with this idea would be appreciated also.
As far as normalizing the DB, for >most< situations, it is best to keep category/subcategory data in one table. That is, they are all categories. Some just have parent categories. That allows the DB to easily accomodate an infinite number of levels in your hierarchy. You might want to impose a two-level limit in your programming code. But, should you later need another level, there will not be any rearranging needed in the DB.
As for writing the hierarchy to a file and then including it, that is a good idea. If editing the categories is done infrequently, or is limited to an Admin, then the file rewrite could be added to the end of your code that saves the category data.
Some just have parent categories.
I've been using that system for quite a while and I do think it's best. It requires that you add one column to your database and that you completely redo your data, though. It will be normalized, but all in one table, like so
cat_id ¦ cat_name ¦ cat_parent_id ¦ cat_url ¦cat_other_stuff
1 ¦ top cat ¦ 0 ¦ url.php ¦ other stuff
2 ¦ sub_cat1 ¦ 1 ¦ url2.php¦ other stuff
3 ¦ sub_cat2 ¦ 2 ¦ url3.php¦ other stuff