I was looking around SO and was wondering how would I go about in creating a related or similar posts to display on my web site using PHP & MySQL? What is a basic example of how to do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Using the MySQL Full Text search
MATCH (col1,col2,...) AGAINST (expr [search_modifier])thing.Let’s say your table is
articlesand you need to find related posts about a title of current post. Do it like this:This will give you top 5 related posts.
But first remember to enabled Full Text search for that table’s columns, by running this query:
[EDIT]: Why to not use
LIKE: Clarification to OP:Because it will not give correct results. Let’s say you current title is “Music of 1980” and you want related posts on that. Now, if you use
LIKEthen only the posts containing EXACTLY the sequence of words “Music of 1980” will appear. However, if you useMATCH ... AGAINST, then posts that contain Music OR 1980 will appear. Also, the posts that contain both Music and 1980 will appear on Top because it gives aSCOREto each results and we are sorting by that score.I hope that’s clear.[EDIT]: 2:
If you have categories, you can add
AND Category = '$CurrentCategory'in the SQL querywhere clauseto get more specific results.[EDIT]: 3: OP can’t use Full text:
If you can not use Full Text (for some reason), you can just show 5 random posts from the same category. As they are in same category, they are somehow related at least:
Edited Syntax: Changed LIMTI to LIMIT in MySQL Code