How to add manually related posts to a post?

Total Post:101

Points:709
PHP 
PHP
 2194  View(s)
Ratings:
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

  1. Post:604

    Points:4228
    Re: How to add manually related posts to a post?

    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.

Answer