Home > DeveloperSection > Forums > How to add manually related posts to a post?
Pravesh Singh
Pravesh Singh

Total Post:101

Points:709
Posted on    May-10-2013 2:43 AM

 PHP PHP 
Ratings:


 1 Reply(s)
 1432  View(s)
Rate this:
Hi Guys

I am designing a blog site in php where users can add manually related posts to a post.

I thought to use an auto increment integer for all posts related.

This is the table:

Blog:
-id
-related ( it's 0 if there aren't related posts or it contain an auto incremet integer that it is the same for all posts related )
-id_user
-title
-body
-date
I prefer not to use a "related table" if possible to avoid complex queries.

So I need a way to get an auto increment integer in this case or you can suggest me other solutions :)

Thanks



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-10-2013 9:18 AM

Hi Pravesh!

Why not have a second table? It's probably the most robust solution, and it doesn't complicity your queries much at all.

You should have a second table, related-posts, with two fields: original and related.

Neither are unique, so the following is possible (assuming 1,2,3,4 are post IDs)

o | r
----
1 | 2
1 | 3
2 | 3
2 | 4
4 | 1
When you need to find posts related to post 1 (for example), you query

SELECT `related` FROM `related-posts` WHERE `original` = 1;
This will give you all related post IDs.

This kind of relationship is called many-to-many relationship, one post can have many related posts, and one post can be related to many original posts.


Don't want to miss updates? Please click the below button!

Follow MindStick