Jump to content


Photo

Creating a simple(?) categorical system using PHP and MySQL


  • Please log in to reply
2 replies to this topic

#1 g23armani

g23armani
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 13 April 2006 - 07:50 PM

I'm working on a portion of my site that allows people to search through a series of articles. I want to sort the articles by category. The problem is, most articles can be placed into more than one category. For example, if I write an article: "Rock Climbing Techniques to Prevent Injury and Increase Strength" I would want this article to be coded under:

Rock Climbing
Injury Prevention
Strength Training

While I could manually place the article in each of it's categories, I'm sure there is a simple way to use PHP and MySQL to assign multiple categories to each article and then have these articles pulled if someone clicks on the appropriate category or searches for the right keyword.

If anyone knows of a good website, book, free code or script source etc that they could point me to that would be tremendously helpful.

Thanks!

Peter

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 13 April 2006 - 10:35 PM

Whenever you have a situation like yours where

1 book can belong to many categories
1 category can contain many books

you have what is known as a "many-to-many" relationship.

To resolve this you create an intermediate link table "article_category", for instance.

Category
----------
1 | Rock Climbing
2 | Injury Prevention
3 | Strength Training

Article
-------------
1 | Rock Climbing Techniques to Prevent Injury and Increase Strength
2 | Climbing for Dummies


Article_category
---------------------
catid | articleid

1 | 1
2 | 1
3 | 1
1 | 2
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 g23armani

g23armani
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 14 April 2006 - 07:09 PM

Thanks, that should help quite a bit.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users