[thelist] PHP, MySQL - category counter puzzle

Tobyn Baugher trb at cartoonviolence.net
Mon May 28 07:12:04 CDT 2001


First, take note that you should take all this advice with several
grains of salt. I'm not a professional db guru. I haven't written or
designed db code for any high profile sites with lots of visitors. I
have never taken a class on db design and I don't even own a really good
book on db design (though I'd be all ears if someone can recommend one
for my already-straining bookshelf). That said:

On Mon, May 28, 2001 at 11:10:32AM +0200, Quinn Comendant wrote:
> - A column in the categories table that would be responsible for 
> containing a total count of all products contained in all the categories 
> under it.

I dislike this approach, simply because I dislike having to operate on
two objects to add/delete one. I'd rather maintain a table of their
associations, that way you can operate on those individually without
potentially affecting either object.

Give each product an ID and each category an ID. Create a table with two
columns, one for each. When you add an item, insert rows linking its IDs
to the IDs of the categories it lives in. Then, to find out how many IDs
are in each you need only query the db for how many rows have a category
ID of X.

Like I said, be wary of my advice, and if I'm just horribly off the mark
somebody correct me so I can learn and improve my own code as well.

Regards,
Toby

-- 
Tobyn "trb" Baugher <trb at cartoonviolence.net>
http://www.cartoonviolence.net
AIM:unlewp ICQ:14281524 EFnet:trb




More information about the thelist mailing list