Forum Moderators: coopster

Message Too Old, No Replies

Code practices

how bad is it to...

         

Code Sentinel

4:37 am on May 8, 2004 (gmt 0)

10+ Year Member



I got a single table with a main category and sub categories. I know I should normalize but I do it this way for ease of maintenance with my current knowledge :)

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.

moltar

5:10 am on May 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What do you mean underneath? In the following rows or in one big blob?

What role does the database play in your system? Does it add any other value except menu hierarchy?

Code Sentinel

6:57 am on May 8, 2004 (gmt 0)

10+ Year Member



It's basically one big blob..

The database has all information in one table.. like 14 fields worth.. I haven't normalized it. It's a read only database.

It took some trial and error but I got it working, I was just wondering if loops within loops are considered bad in any way :)

john_k

7:17 am on May 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you mean that you hit the DB with each pass through the loop? If so, then yes, you should be able to get it all with a single SELECT. Just use an ORDER BY clause to bring back the rows in the correct order.

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.

ergophobe

2:46 pm on May 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




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