Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 17th, 2007, 2:17 PM   #1
gj15987
Programmer
 
Join Date: Feb 2005
Posts: 47
Rep Power: 0 gj15987 is on a distinguished road
Categorizing

I'm in the process of making a website in php on Reptiles and Amphibians. Just using free hosting while I'm making it. Address is http://herp.awardspace.com.

At the moment, I can add animals to the database and upload articles and those are searchable using the search box near the top right hand side.

I have one problem though. How do I categorize the animals?

Lets say I want to find a Royal Python...I'd put it under Reptiles > Snakes > Non-Venemous > Pythons.

On the site, if you navigate through all those it will indeed show a royal python. But I need it to also show up in the higher categories before that specific one. eg. The royal python will show up with all boa constrictors when viewing the non venemous category.

As it stands I have a category field in each animal record where I put its lowest category, in this case pythons.

I have a category table: category(id, name, parent). Parent being the parent category of that category.

Obviously this is not the right/ best way to do it so if any of that makes sense and anyone has any ideas I'd be greatful to hear them.



Thanks.

Last edited by gj15987; Jan 17th, 2007 at 2:38 PM.
gj15987 is offline   Reply With Quote
Old Jan 17th, 2007, 2:42 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by gj15987 View Post
As it stands I have a category field in each animal record where I put its lowest category, in this case pythons.

I have a category table: category(id, name, parent). Parent being the parent category of that category.
Remove the category field from the animals table, and add in a linking table that connects categories to animals.

e.g.
category_animal(category_id, animal_id)
Arevos is offline   Reply With Quote
Old Jan 17th, 2007, 5:22 PM   #3
gj15987
Programmer
 
Join Date: Feb 2005
Posts: 47
Rep Power: 0 gj15987 is on a distinguished road
Ah, very good idea! Thank you very much!

So if I do it that way...what would be the best way to add the animal in all the categories?

Eg. If i add a royal python in pythons, code would look up pythons category and also add it in the parent category, and then also in the parent of that one too etc until the top category is reched?
gj15987 is offline   Reply With Quote
Old Jan 17th, 2007, 6:18 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You might want to do some research into relational database design, perhaps, specifically, entity-relationship modeling. I have little doubt that the effort would result in a higher level of satisfaction with your design.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 17th, 2007, 6:45 PM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
A for loop with multiple insert statements. Presumably you're going to be reading from the database far more than writing to it, so a for-loop, though less efficient than a single insert query, seems the most efficient way overall.

You could set up a table to store all an animal's ancestor categories, I guess, something like:
ancestors(category_id, ancestor_id)

But that's just trading memory for CPU, and considering each animal is probably only going to belong to a relatively small group of categories, it's probably not worth it. Go with a for-loop and multiple inserts, would be my advice.
Arevos is offline   Reply With Quote
Old Jan 18th, 2007, 1:25 AM   #6
gj15987
Programmer
 
Join Date: Feb 2005
Posts: 47
Rep Power: 0 gj15987 is on a distinguished road
Thank you for everyone's help.

I took out the category field in the animal table. Created a link table category_animal(category id, animal id). And when adding a new animal it asks the user for its lowest category, (eg python and not just snakes), and it adds it in pythons, then looks at pythons parent and adds it there, and so on until it gets to a top level category!
gj15987 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:42 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC