Related posts or You May Also Like for Your Website




1.89 K Views | Rishi Kashyap | 1 FOLLOW
    X

    Related posts or You May Also Like for Your Website


    0

    I first saw Related Posts or "You may also like" in a wordpress Blog. At the End of the blog post "You may also Like" suggested other related posts of similar or almost similar blog topics. So here is how you can implement "You may also Like" for your website.

    This post will detail the basic Idea of how "You may also Like" works with the related posts. I am not writing the Complete Code of it as everyone will have his own table structure in the Database but using this approach you can use any Server Side Language to connect the Tables.
    For simplicity I am using 2 tables to explain, of course you can add additional tables as per your need.

    In Table 1 (Related Posts) we are tagging each Post. This tag is done as per particular category chosen and named by the author. The joining Column will be the blog_id (foreign key).
    In Table 2 (Posts) each blog have its title, detail and blog_id. The blog_id as told before is the foreign key and will join both tables to get the data we need later.

    Lets assume you have opened the topic "Adding Google Analytics Code" and read the complete post in your browser. At the end you find "You may also Like" and few suggestion of other related Posts.
    From Table 2 you can see that the blog_id for "Adding Google Analytics Code" is 2 and from Table 1 you can find out the tags for blog_id of 2 are "GOOGLE ANALYTICS" and "START WEBSITE". Now all you have to do is find the blog_id with tags of "GOOGLE ANALYTICS" and "START WEBSITE" and removing blog_id of 2. The answer are 1, 3, 5, 6 and 7. So you have 5 topics with the same tags and probably that is what "you may also like".
    Once you have the blog_id, get the link for those topics and generate the same using any server side language of your choice. Below I am showing the SQL query with PHP.

    I do understand the below SQL query which I am suggesting might not be good but it does the work for me ! Your views to improve it are always welcomed.


    $view_post='2';
    //$con_data will be your Database connection setting
    //We will first get the tags for the above mentioned Post
    $query="SELECT tag FROM Related_Posts WHERE blog_id='$view_post'";
    $result= @mysql_query($query,$con_data);
    while($tagdata = @mysql_fetch_array ($result))
    {$ymal[]= $tagdata['tag'];}

    //Now with the various Tag details we will generate the Related Posts under "You May also Like"
    foreach($ymal as $ymal1)
    {
    //Select max 10 posts only
    $query="SELECT DISTINCT Blog_Title,Related_Posts.blog_id FROM Related_Posts,Posts WHERE Related_Posts.blog_id != '$view_post' AND Related_Posts.blog_id=Posts.blog_id AND tag='$ymal1' $add LIMIT 10";
    $result= @mysql_query($query,$con_data);
    while($mydatal = @mysql_fetch_array ($result))
    {$title=$mydatal['Blog_Title'];
    $id=$mydatal['blog_id'];
    $data["$id"]="$title";
    //Remove this blog_id from later queries for other tags
    $add .=" AND Related_Posts.blog_id != '$id'";}
    }

    For each Tag we have got Maximum 10 Posts, now we will generate the "You May also like" and select max 5 posts at random
    if(count($data)>0){
    //Get random keys from the total collected data
    if(count($data)>5){$nu='5';} else {$nu=count($data);}
    //If $nu is more than the total number of data then it will not output anything so limit to max data only
    $data1=array_rand($data,$nu);
    echo "You may also Like :";
    foreach($data1 as $b_id){echo "$data[$b_id]";}

    Of course you will have to create the link of the Related Posts not just the Title, so modify it as required. Hope you may also like the suggestions of related posts below ;-)Related posts or You May Also Like for Your Website

    Rishi Kashyap | 8 years ago | EDIT | REPLY



      YOU MAY ALSO LIKE
      tags  PHP TUTORIAL WEBSITE BASICS