Jump to content

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


g23armani

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.