Monday 7 January 2019

Twitter Like Follow Unfollow System in PHP using Ajax jQuery



From this post, you can learn how to make Follow and Unfollow system like Twitter which you can create in PHP using Ajax jQuery Mysql and Bootstrap. There are many viewers has requested to use PHP to build a Twitter like system. If you know on Twitter can post short News in Tweets format, and that tweets will be visible to those Twitter user who has follow this share user. When user share any news or tweets, that will be visible to his followers. So, this type of system we will make in PHP using Ajax jQuery and Mysql Database.

IF you are looking for any Social Networking Application for you colleage project, Then this post will help you to make small Follow Unfollow Application in PHP using Ajax step by step. On every publish of video tutorial of this post, you can find updated source code under this post. So, here we will show you How to develop twitter like follow and Unfollow system using Ajax in PHP script. Because most of this Social Networking sites are make in PHP Ajax. Here we have also use Bootstrap library for CSS purpose for make this system. So, here we will make dynamic follow unfollow application in PHP using Ajax.








Following are the main functionality of this follow unfollow system.

  • New User Registration
  • Login form for registered user
  • Authenticated User can edit his or here profile with upload profile image
  • User can share post, and list post
  • List User with follow unfollow button
  • Display number of followers of each user
  • Main functionality is to follow or unfollow other user post
  • Display dynamic results will be display if user click on follow unfollo button
  • All data will display using Ajax without refresh of web page
  • If you user follow any user, then he can also unfollow that user again
  • User can view follow user post on their timeline
  • User can comment on other user post
  • User can view comment on post
  • User can repost other user post on their timeline
  • User can see how many time particular post has been reposted
  • User can like other user post
  • User can like particular post only one time
  • User can view number of like on particular post
  • User can view username who has like post in Bootstrap tooltip
  • Share image or Video with Post
  • URL Content Extract with preview
  • Search User feature
  • View all post of single user on one page

So, Above are the main functionality of this follow unfollow system in PHP using Ajax jQuery Mysql and Bootstrap. Below you can find source code of Ajax based follow unfollow system.



Video Tutorial


Part 1




Part 2




Part 3




Part 4




Part 5




Part 6




Part 7




Part 8




Part 9




Part 10




Part 11




Part 12



Source Code


Database


Run follow SQL script, it will make three table like tbl_twitter_user, tbl_samples_post and tbl_follow in your PHPMysqmin. Follow Unfollow system has been use this three table for make application.


CREATE TABLE `tbl_follow` (
  `follow_id` int(11) NOT NULL AUTO_INCREMENT,
  `sender_id` int(11) NOT NULL,
  `receiver_id` int(11) NOT NULL,
  PRIMARY KEY (`follow_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

CREATE TABLE `tbl_samples_post` (
  `post_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `post_content` text NOT NULL,
  `post_datetime` datetime NOT NULL,
  PRIMARY KEY (`post_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

CREATE TABLE `tbl_twitter_user` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(150) NOT NULL,
  `password` varchar(150) NOT NULL,
  `name` varchar(150) NOT NULL,
  `profile_image` varchar(150) NOT NULL,
  `bio` text NOT NULL,
  `follower_number` int(11) NOT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

CREATE TABLE `tbl_comment` (
  `comment_id` int(11) NOT NULL AUTO_INCREMENT,
  `post_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `comment` text NOT NULL,
  `timestamp` datetime NOT NULL,
  PRIMARY KEY (`comment_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;

CREATE TABLE `tbl_like` (
  `like_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `post_id` int(11) NOT NULL,
  PRIMARY KEY (`like_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;



database_connection.php


This PHP file is used for make database connection. This is common file which we will used in all files for make Mysql Database connection.


<?php

//database_connection.php

$connect = new PDO("mysql:host=localhost;dbname=testing", "root", "");

function Count_notification($connect, $receiver_id)
{
 $query = "
 SELECT COUNT(notification_id) as total 
 FROM tbl_notification 
 WHERE notification_receiver_id = '".$receiver_id."' 
 AND read_notification = 'no'
 ";

 $statement = $connect->prepare($query);

 $statement->execute();

 $result = $statement->fetchAll();

 foreach($result as $row)
 {
  return $row["total"];
 }
}

function Load_notification($connect, $receiver_id)
{
 $query = "
 SELECT * FROM tbl_notification 
 WHERE notification_receiver_id = '".$receiver_id."' 
 ORDER BY notification_id DESC
 ";
 $statement = $connect->prepare($query);

 $statement->execute(); 

 $result = $statement->fetchAll();

 $total_row = $statement->rowCount();

 $output = '';

 if($total_row > 0)
 {
  foreach($result as $row)
  {
   $output .= '<li><a href="#">'.$row["notification_text"].'</a></li>';
  }
 }
 return $output;
}

function Get_user_name($connect, $user_id)
{
 $query = "
 SELECT username FROM tbl_twitter_user 
 WHERE user_id = '".$user_id."'
 ";

 $statement = $connect->prepare($query);

 $statement->execute();

 $result = $statement->fetchAll();

 foreach($result as $row)
 {
  return $row["username"];
 }
}

function count_retweet($connect, $post_id)
{
 $query = "
 SELECT * FROM tbl_repost 
 WHERE post_id = '".$post_id."'
 ";
 $statement = $connect->prepare($query);
 $statement->execute();
 return $statement->rowCount();
}

function count_comment($connect, $post_id)
{
 $query = "
 SELECT * FROM tbl_comment 
 WHERE post_id = '".$post_id."'
 ";
 $statement = $connect->prepare($query);
 $statement->execute();
 return $statement->rowCount();
}

function make_follow_button($connect, $sender_id, $receiver_id)
{
 $query = "
 SELECT * FROM tbl_follow 
 WHERE sender_id = '".$sender_id."' 
 AND receiver_id = '".$receiver_id."'
 ";
 $statement = $connect->prepare($query);
 $statement->execute();
 $total_row = $statement->rowCount();
 $output = '';
 if($total_row > 0)
 {
  $output = '<button type="button" name="follow_button" class="btn btn-warning action_button" data-action="unfollow" data-sender_id="'.$sender_id.'"> Following</button>';
 }
 else
 {
  $output = '<button type="button" name="follow_button" class="btn btn-info action_button" data-action="follow" data-sender_id="'.$sender_id.'"><i class="glyphicon glyphicon-plus"></i> Follow</button>';
 }
 return $output;
}

function count_total_post_like($connect, $post_id)
{
 $query = "
 SELECT * FROM tbl_like 
 WHERE post_id = '".$post_id."'
 ";

 $statement = $connect->prepare($query);

 $statement->execute();

 return $statement->rowCount();
}

function Get_user_id($connect, $username)
{
 $query = "
 SELECT user_id FROM tbl_twitter_user 
 WHERE username = '".$username."'
 "; 
 $statement = $connect->prepare($query);
 $statement->execute();
 $result = $statement->fetchAll();
 foreach($result as $row)
 {
  return $row["user_id"];
 }
}


?>




register.php


For any dynamic web application, we have to first make register form. So, here also first we want to make user registration system. So, new user can register into system. So, for make any dynamic system, here we have make this register form. In this system, here we have validate username already exists or not. If username already exists then user cannot register into system. In this registration system we have also validate user password by re-entering password. So, this is basic validation, which we have use here for make registration system for Follow Unfollow system using PHP with Ajax jQuery Mysql and Bootstrap.


<!--
//register.php
!-->

<?php

include('database_connection.php');

session_start();

$message = '';

if(isset($_SESSION['user_id']))
{
 header('location:index.php');
}

if(isset($_POST['register']))
{
 $username = trim($_POST["username"]);
 $password = trim($_POST["password"]);
 $check_query = "
 SELECT * FROM tbl_twitter_user 
 WHERE username = :username
 ";
 $statement = $connect->prepare($check_query);
 $check_data = array(
  ':username'  => $username
 );
 if($statement->execute($check_data))
 {
  if($statement->rowCount() > 0)
  {
   $message .= '<p><label>Username already taken</label></p>';
  }
  else
  {
   if(empty($username))
   {
    $message .= '<p><label>Username is required</label></p>';
   }
   if(empty($password))
   {
    $message .= '<p><label>Password is required</label></p>';
   }
   else
   {
    if($password != $_POST["confirm_password"])
    {
     $message .= '<p><label>Password not match</label></p>';
    }
   }
   if($message == '')
   {
    $data = array(
     ':username'  => $username,
     ':password'  => password_hash($password, PASSWORD_DEFAULT)
    );

    $query = "
    INSERT INTO tbl_twitter_user 
    (username, password) 
    VALUES (:username, :password)
    ";

    $statement = $connect->prepare($query);

    if($statement->execute($data))
    {
     $message = '<label>Registration Completed</label>';
    }
   }
  }
 }
}

?>

<html>  
    <head>  
        <title>Twitter Like Follow Unfollow System in PHP using Ajax jQuery</title>  
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    </head>  
    <body>  
        <div class="container">
   <br />
   
   <h3 align="center">Twitter Like Follow Unfollow System in PHP using Ajax jQuery</a></h3><br />
   <br />
   <div class="panel panel-default">
      <div class="panel-heading">Register</div>
    <div class="panel-body">
     <form method="post">
      <span class="text-danger"><?php echo $message; ?></span>
      <div class="form-group">
       <label>Enter Username</label>
       <input type="text" name="username" class="form-control" />
      </div>
      <div class="form-group">
       <label>Enter Password</label>
       <input type="password" name="password" id="password" class="form-control" />
      </div>
      <div class="form-group">
       <label>Re-enter Password</label>
       <input type="password" name="confirm_password" id="confirm_password" class="form-control" />
      </div>
      <div class="form-group">
       <input type="submit" name="register" class="btn btn-info" value="Register" />
      </div>
      <div align="center">
       <a href="login.php">Login</a>
      </div>
     </form>
    </div>
   </div>
  </div>
    </body>  
</html>


login.php


Once you have make registration system in Follow Unfollow application. After registration you have to make login page for authenticate user login details like username and password. Here password will be validate by using password_hash() method. Because at the time registration we have store password in hash format. If user has enter proper details, then his for validate user is login or not in whole system, we have store user details under $_SESSION variable.


<!--
//login.php
!-->

<?php

include('database_connection.php');

session_start();

$message = '';

if(isset($_SESSION['user_id']))
{
 header('location:index.php');
}

if(isset($_POST["login"]))
{
 $query = "
 SELECT * FROM tbl_twitter_user 
    WHERE username = :username
 ";
 $statement = $connect->prepare($query);
 $statement->execute(
  array(
      ':username' => $_POST["username"]
     )
 );
 $count = $statement->rowCount();
 if($count > 0)
 {
  $result = $statement->fetchAll();
  foreach($result as $row)
  {
   if(password_verify($_POST['password'], $row['password']))
   {
    $_SESSION['user_id'] = $row['user_id'];
    $_SESSION['username'] = $row['username'];
    header('location:index.php');
   }
   else
   {
    $message = '<label>Wrong Password</label>';
   }
  }
 }
 else
 {
  $message = '<label>Wrong Username</labe>';
 }
}


?>

<html>  
    <head>  
        <title>Twitter Like Follow Unfollow System in PHP using Ajax jQuery</title>  
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    </head>  
    <body>  
        <div class="container">
   <br />
   
   <h3 align="center">Twitter Like Follow Unfollow System in PHP using Ajax jQuery</a></h3><br />
   <br />
   <div class="panel panel-default">
      <div class="panel-heading">Login</div>
    <div class="panel-body">
     <form method="post">
      <p class="text-danger"><?php echo $message; ?></p>
      <div class="form-group">
       <label>Enter Username</label>
       <input type="text" name="username" class="form-control" required />
      </div>
      <div class="form-group">
       <label>Enter Password</label>
       <input type="password" name="password" class="form-control" required />
      </div>
      <div class="form-group">
       <input type="submit" name="login" class="btn btn-info" value="Login" />
      </div>
      <div align="center">
       <a href="register.php">Register</a>
      </div>
     </form>
    </div>
   </div>
  </div>
    </body>  
</html>


index.php


If system is validated user information, and it is correct then page will be redirect to index.php page. From this page user can share his or her post. User can view his or her post along with post of follow user by login user. On index page user can view all user list with follow and unfollow button. Most of all operation of follow and unfollow system like post new post, comment on other user post, follow and unfollow other user, repost follow yser post will be done on this page. This page will be view to only login user.


<!--
//index.php
!-->

<?php

include('database_connection.php');

session_start();

if(!isset($_SESSION['user_id']))
{
    header('location:login.php');
}

?>

<html>  
    <head>  
        <title>Twitter Like Follow Unfollow System in PHP using Ajax jQuery</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.2/jquery.form.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"></script>
        <style>

        .main_division
        {
            position: relative;
            width: 100%;
            height: auto;
            background-color: #FFF;
            border: 1px solid #CCC;
            border-radius: 3px;
        }
        #sub_division
        {
            width: 100%;
            height: auto;
            min-height: 80px;
            overflow: auto;
            padding:6px 24px 6px 12px;
        }
        .image_upload
        {
            position: absolute;
            top:0px;
            right:16px;
        }
        .image_upload > form > input
        {
            display: none;
        }

        .image_upload img
        {
            width: 24px;
            cursor: pointer;
        }

        </style>
    </head>  
    <body>  
        <div class="container">
   <?php
            include('menu.php');
            ?>
            <div class="row">
                <div class="col-md-8">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <div class="row">
                                <div class="col-md-8">
                                    <h3 class="panel-title">Start Write Here</h3>
                                </div>
                                <div class="col-md-4">
                                    <div class="image_upload">
                                        <form id="uploadImage" method="post" action="upload.php">
                                            <label for="uploadFile"><img src="upload.png" /></label>
                                            <input type="file" name="uploadFile" id="uploadFile" accept=".jpg, .png, .mp4" />
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="panel-body">
                            <form method="post" id="post_form">
                                <div class="form-group" id="dynamic_field">
                                    <textarea name="post_content" id="post_content" maxlength="160" class="form-control" placeholder="Write your short story"></textarea>
                                </div>
                                <div id="link_content"></div>
                                <div class="form-group" align="right">
                                    <input type="hidden" name="action" value="insert" />
                                    <input type="hidden" name="post_type" id="post_type" value="text" />
                                    <input type="submit" name="share_post" id="share_post" class="btn btn-primary" value="Share" />
                                </div>
                            </form>
                        </div>
                    </div>
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h3 class="panel-title">Trending Now</h3>
                        </div>
                        <div class="panel-body">
                            <div id="post_list">

                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h3 class="panel-title">User List</h3>
                        </div>
                        <div class="panel-body">
                            <div id="user_list"></div>
                        </div>
                    </div>
                </div>
            </div>
  </div>
    </body>  
</html>

<?php

include('jquery.php');

?>


jquery.php



<script>  
$(document).ready(function(){
 
 $('#post_form').on('submit', function(event){
        event.preventDefault();
        if($('#post_type').val() == 'upload')
        {
            $('#post_content').val($('#sub_division').html());
        }

        if($('#post_type').val() == 'link')
        {
            $('#post_content').val($('#link_content').html());
            $('#link_content').css('padding', '0');
            $('#link_content').css('background-color', '');
            $('#link_content').css('margin-bottom', '0');
        }

        if($('#post_content').val() == '')
        {
            alert('Enter Story Content');
        }
        else
        {
            var form_data = $(this).serialize();
            $.ajax({
                url:"action.php",
                method:"POST",
                data:form_data,
                beforeSend:function()
                {
                    $('#share_post').attr('disabled', 'disabled');  
                },
                success:function(data)
                {
                    //alert('Post has been shared');
                    $('#dynamic_field').html('<textarea name="post_content" id="post_content" maxlength="160" class="form-control" placeholder="Write your short story"></textarea>');
                    $('#post_type').val('text');
                    $('#post_form')[0].reset();
                    fetch_post();
                    $('#link_content').html('');
                    $('#share_post').attr('disabled', false);
                }
            })
        }
    });

    fetch_post();

    function fetch_post()
    {
       var action = 'fetch_post';
       $.ajax({
            url:'action.php',
            method:"POST",
            data:{action:action},
            success:function(data)
            {
                $('#post_list').html(data);
            }
       })
    }

    fetch_user();

    function fetch_user()
    {
        var action = 'fetch_user';
        $.ajax({
            url:"action.php",
            method:"POST",
            data:{action:action},
            success:function(data)
            {
                $('#user_list').html(data);
            }
        });
    }

    $(document).on('click', '.action_button', function(){
        var sender_id = $(this).data('sender_id');
        var action = $(this).data('action');
        $.ajax({
            url:"action.php",
            method:"POST",
            data:{sender_id:sender_id, action:action},
            success:function(data)
            {
                fetch_user();
                fetch_post();
            }
        })
    });

    var post_id;
    var user_id;

    $(document).on('click', '.post_comment', function(){
        post_id = $(this).attr('id');
        user_id = $(this).data('user_id');
        var action = 'fetch_comment';
        $.ajax({
            url:"action.php",
            method:"POST",
            data:{post_id:post_id, user_id:user_id, action:action},
            success:function(data){
                $('#old_comment'+post_id).html(data);
                $('#comment_form'+post_id).slideToggle('slow');
            }
        })
        
    });

    $(document).on('click', '.submit_comment', function(){
        var comment = $('#comment'+post_id).val();
        var action = 'submit_comment';
        var receiver_id = user_id;
        if(comment != '')
        {
            $.ajax({
                url:"action.php",
                method:"POST",
                data:{post_id:post_id,receiver_id:receiver_id,comment:comment,action:action},
                success:function(data)
                {
                    $('#comment_form'+post_id).slideUp('slow');
                    fetch_post();
                }
            })
        }
    });

    $(document).on('click', '.repost', function(){
        var post_id = $(this).data('post_id');
        var action = 'repost';
        $.ajax({
            url:"action.php",
            method:"POST",
            data:{post_id:post_id, action:action},
            success:function(data)
            {
                alert(data);
                fetch_post();
            }
        })
    });

    $(document).on('click', '.like_button', function(){
        var post_id = $(this).data('post_id');
        var action = 'like';
        $.ajax({
            url:"action.php",
            method:"POST",
            data:{post_id:post_id, action:action},
            success:function(data)
            {
                alert(data);
                fetch_post();
            }
        })
    });

    $('body').tooltip({
        selector: '.like_button',
        title: fetch_post_like_user_list,
        html: true,
        placement: 'right'
    });

    function fetch_post_like_user_list()
    {
        var fetch_data = '';
        var element = $(this);
        var post_id = element.data('post_id');
        var action = 'like_user_list';
        $.ajax({
            url:"action.php",
            method:"POST",
            async: false,
            data:{post_id:post_id, action:action},
            success:function(data)
            {
                fetch_data = data;
            }
        });
        return fetch_data;
    }

    $('#uploadFile').on('change', function(event){
        var html = '<div class="main_division">';
        html += '<div id="sub_division" contenteditable class="form-control"></div></div>';
        html += '<input type="hidden" name="post_content" id="post_content" />';
        $('#post_type').val('upload');
        $('#dynamic_field').html(html);
        $('#uploadImage').ajaxSubmit({
            target :'#sub_division',
            resetForm :true
        });
    });

    $(document).on('keyup', '#post_content', function(){
        var check_content = $('#post_content').val();
        var check_url = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;

        var if_url = check_content.match(check_url);

        if(if_url)
        {
            $('#link_content').css('padding', '16px');
            $('#link_content').css('background-color', '#f9f9f9');
            $('#link_content').css('margin-bottom', '16px');
            $('#link_content').html('<h4>Fetching...</h4>');
            $('#post_type').val('link');
            var action = 'fetch_link_content';
            $.ajax({
                url:"action.php",
                method:"POST",
                data:{action:action, url:if_url},
                success:function(data)
                {
                    var title = $(data).filter("meta[property='og:title']").attr('content');
                    var description = $(data).filter("meta[property='og:description']").attr('content');

                    var image = $(data).filter("meta[property='og:image']").attr('content');

                    if(title == undefined)
                    {
                        title = $(data).filter("meta[name='twitter:title']").attr('content');
                    }

                    if(description == undefined)
                    {
                        description = $(data).filter("meta[name='twitter:description']").attr('content');
                    }

                    if(image == undefined)
                    {
                        image = $(data).filter("meta[name='twitter:image']").attr('content');
                    }

                    var output = '<p><a href="'+if_url[0]+'">'+if_url[0]+'</a></p>';

                    output += '<img src="'+image+'" class="img-responsive img-thumbnail" />';
                    output += '<h3><b>'+title+'</b></h3>';
                    output += '<p>'+description+'</p>';
                    $('#link_content').html(output);
                }
            })
        }
        else
        {
            $('#link_content').html('');
            $('#link_content').css('padding', '0');
            $('#link_content').css('background-color', '');
            $('#link_content').css('margin-bottom', '');
            return false;
        }
    });

    $('#view_notification').click(function(){
        var action = 'update_notification_status';
        $.ajax({
            url:"action.php",
            method:"post",
            data:{action:action},
            success:function(data)
            {
                $('#total_notification').remove();
            }
        })
    });
 
});  
</script>


menu.php


This file is used for make dynamic menu, here user name will be display by using $_SESSION varibale. Here in this we have add two menu for go to profile page and logout page.


<br />
   <nav class="navbar navbar-inverse">
    <div class="container-fluid">
     <div class="navbar-header">
      <a class="navbar-brand" href="index.php">Webslesson</a>
     </div>
     <ul class="nav navbar-nav navbar-right">
      <li>
       <input type="text" name="search_user" id="search_user" class="form-control input-sm" placeholder="Search User" autocomplete="off" style="margin-top: 10px; width: 400px; margin-right:180px;" />
      </li>
      <li class="dropdown">
       <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="view_notification">Notification
       <?php
       $total_notification = Count_notification($connect, $_SESSION["user_id"]);

       if($total_notification > 0)
       {
        echo '<span class="label label-danger" id="total_notification">'.$total_notification.'</span>';

       }


       ?>
        <span class="caret"></span></a>
        <ul class="dropdown-menu">
        <?php
        echo Load_notification($connect, $_SESSION["user_id"]);

        ?>

        </ul>
       </a>
      </li>
      <li class="dropdown">
       <a class="dropdown-toggle" data-toggle="dropdown" href="#"><?php echo $_SESSION['username']; ?>
       <span class="caret"></span></a>
       <ul class="dropdown-menu">
        <li><a href="profile.php">Profile</a></li>
        <li><a href="logout.php">Logout</a></li>
       </ul>
      </li>
     </ul>
    </div>
   </nav>

   <script type="text/javascript">

    $(document).ready(function(){
     $('#search_user').typeahead({
      source:function(query, result)
      {
       $('.typeahead').css('position', 'absolute');
       $('.typeahead').css('top', '45px');
       var action = 'search_user';
       $.ajax({
        url:"action.php",
        method:"POST",
        data:{query:query, action:action},
        dataType:"json",
        success:function(data)
        {
         result($.map(data, function(item){
          return item;
         }));
        }
       })
      }
     });

     $(document).on('click', '.typeahead li', function(){
      var search_query = $(this).text();
      window.location.href="wall.php?data="+search_query;
     });
    });

   </script>


logout.php


If user want to logout from dynamic follow unfollow application, so when user click on logout link then page will redirect to this page, and on this page all $_SESSION variable will be destroy using session_destroy() function.


<?php

//logout.php

session_start();

session_destroy();

header('location:login.php');

?>


profile.php


This file is used for edit user profile details in follow unfollow application by using PHP. Here user not only edit their profile details like username, password, name and short bio but also user can change profile image by upload image file under images folder. This image and user name will be display to other user.


<!--
//profile.php
!-->

<?php

include('database_connection.php');

session_start();

if(!isset($_SESSION['user_id']))
{
 header('location:login.php');
}

$message = '';

if(isset($_POST['edit_profile']))
{
 $file_name = '';
 if(isset($_POST['profile_image']))
 {
  $file_name = $_POST['profile_image'];
 }

 if($_FILES['profile_image']['name'] != '')
 {
  if($file_name != '')
  {
   unlink('images/'.$file_name);
  }
  $image_name = explode(".", $_FILES['profile_image']['name']);
  $extension = end($image_name);
  $temporary_location = $_FILES['profile_image']['tmp_name'];
  $file_name = rand() . '.' . strtolower($extension);
  $location = 'images/' . $file_name;
  move_uploaded_file($temporary_location, $location);
 }
 $check_query = "
 SELECT * FROM tbl_twitter_user WHERE username = :username AND user_id != :user_id
 ";
 $statement = $connect->prepare($check_query);
 $statement->execute(
  array(
   ':username'  =>  trim($_POST["username"]),
   ':user_id'  =>  $_SESSION["user_id"]
  )
 );
 $total_row = $statement->rowCount();
 if($total_row > 0)
 {
  $message = '<div class="alert alert-danger">Username Already Exists</div>';
 }
 else
 {
  $data = array(
   ':username'   => trim($_POST["username"]),
   ':name'    => trim($_POST["name"]),
   ':profile_image' => $file_name,
   ':bio'    => trim($_POST["bio"]),
   ':user_id'   => $_SESSION["user_id"]
  );
  if($_POST['password'] != '')
  {
   $data[] = array(
    ':password'  => password_hash($_POST["password"], PASSWORD_DEFAULT)
   );
   $query = '
   UPDATE tbl_twitter_user SET username = :username, password = :password, name = :name, profile_image = :profile_image, bio = :bio WHERE user_id = :user_id
   ';
  }
  else
  {
   $query = '
   UPDATE tbl_twitter_user SET username = :username, name = :name, profile_image = :profile_image, bio = :bio WHERE user_id = :user_id
   ';
  }
  $statement = $connect->prepare($query);
  if($statement->execute($data))
  {
   $message = '<div class="alert alert-success">Profile Updated</div>';
  }
 }
}



$query = "SELECT * FROM tbl_twitter_user WHERE user_id = '".$_SESSION["user_id"]."'";

$statement = $connect->prepare($query);

$statement->execute();

$result = $statement->fetchAll();

?>

<html>  
    <head>  
        <title>Twitter Like Follow Unfollow System in PHP using Ajax jQuery</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.2/jquery.form.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    </head>  
    <body>  
        <div class="container">
   <?php 
   include('menu.php');
   ?>
   <div class="row">
    <div class="col-md-3">
    
    </div>
    <div class="col-md-6">
     <div class="panel panel-default">
      <div class="panel-heading">
       <h3 class="panel-title">Edit Profile</h3>
      </div>
      <div class="panel-body">
       <?php
       foreach($result as $row)
       {
        echo $message;
       ?>
       <form method="post" enctype="multipart/form-data">
        <div class="form-group">
         <label>Username</label>
         <input type="text" name="username" id="username" pattern="^[a-zA-Z0-9_.-]*$" required class="form-control" value="<?php echo $row["username"];?>" />
        </div>
        <div class="form-group">
         <label>Password</label>
         <input type="password" name="password" id="password" class="form-control" />
        </div>
        <div class="form-group">
         <label>Name</label>
         <input type="text" name="name" id="name" class="form-control" required value="<?php echo $row["name"]; ?>" />
        </div>
        <div class="form-group">
         <label>Profile Image</label>
         <input type="file" name="profile_image" id="profile_image" accept="image/*" />
         <?php
         if($row["profile_image"] != '')
         {
          echo '<img src="images/'.$row["profile_image"].'" class="img-thumbnail" width="150" />';
          echo '<input type="hidden" name="profile_image" value="'.$row["profile_image"].'" />';
         }
         ?>
        </div>
        <div class="form-group">
         <label>Short Bio</label>
         <textarea name="bio" id="bio" class="form-control"><?php echo $row["bio"]; ?></textarea>
        </div>
        <div class="form-group">
         <input type="submit" name="edit_profile" id="edit_profile" class="btn btn-primary" value="Save" />
        </div>
       </form>
       <?php
       }
       ?>
      </div>
     </div>
    </div>
    <div class="col-md-3">
    
    </div>
   </div>
  </div>
    </body>  
</html>


action.php


This file is used for perform all core operation of Insert Post, Fetch post data, fetch user data and make follow unfollow button in PHP Follow Unfollow system. This file will received request for all above operation.


<?php

//action.php

include('database_connection.php');

session_start();

if(isset($_POST['action']))
{
 $output = '';
 if($_POST['action'] == 'insert')
 {
  $data = array(
   ':user_id'   => $_SESSION["user_id"],
   ':post_content'  => $_POST["post_content"],
   ':post_datetime' => date("Y-m-d") . ' ' . date("H:i:s", STRTOTIME(date('h:i:sa')))
  );
  $query = "
  INSERT INTO tbl_samples_post 
  (user_id, post_content, post_datetime) 
  VALUES (:user_id, :post_content, :post_datetime)
  ";

  $statement = $connect->prepare($query);
  $statement->execute($data);

  $notification_query = "
  SELECT receiver_id FROM tbl_follow 
  WHERE sender_id = '".$_SESSION["user_id"]."'
  ";

  $statement = $connect->prepare($notification_query);

  $statement->execute();

  $notification_result = $statement->fetchAll();

  foreach($notification_result as $notification_row)
  {
   $notification_text= '<b>' . Get_user_name($connect, $_SESSION["user_id"]) . '</b> has share new post';

   $insert_query = "
   INSERT INTO tbl_notification 
   (notification_receiver_id, notification_text, read_notification) 
   VALUES ('".$notification_row['receiver_id']."', '".$notification_text."', 'no')
   ";

   $statement = $connect->prepare($insert_query);

   $statement->execute();
  }
 }

 if($_POST['action'] == 'fetch_post')
 {
  $query = "
  SELECT * FROM tbl_samples_post 
  INNER JOIN tbl_twitter_user ON tbl_twitter_user.user_id = tbl_samples_post.user_id 
  LEFT JOIN tbl_follow ON tbl_follow.sender_id = tbl_samples_post.user_id 
  WHERE tbl_follow.receiver_id = '".$_SESSION["user_id"]."' OR tbl_samples_post.user_id = '".$_SESSION["user_id"]."' 
  GROUP BY tbl_samples_post.post_id 
  ORDER BY tbl_samples_post.post_id DESC
  ";
  $statement = $connect->prepare($query);
  $statement->execute();
  $result = $statement->fetchAll();
  $total_row = $statement->rowCount();
  if($total_row > 0)
  {
   foreach($result as $row)
   {
    $profile_image = '';
    if($row['profile_image'] != '')
    {
     $profile_image = '<img src="images/'.$row["profile_image"].'" class="img-thumbnail img-responsive" />';
    }
    else
    {
     $profile_image = '<img src="images/user.jpg" class="img-thumbnail img-responsive" />';
    }

    $repost = 'disabled';
    if($row['user_id'] != $_SESSION['user_id'])
    {
     $repost = '';
    }
    $output .= '
    <div class="jumbotron" style="padding:24px 30px 24px 30px">
     <div class="row">
      <div class="col-md-2">
       '.$profile_image.'
      </div>
      <div class="col-md-8">
       <h3><b>@'.$row["username"].'</b></h3>
       <p>'.$row["post_content"].'<br /><br />
       <button type="button" class="btn btn-link post_comment" id="'.$row["post_id"].'" data-user_id="'.$row["user_id"].'">'.count_comment($connect, $row["post_id"]).' Comment</button>
       <button type="button" class="btn btn-danger repost" data-post_id="'.$row["post_id"].'" '.$repost.'><span class="glyphicon glyphicon-retweet"></span>&nbsp;&nbsp;'.count_retweet($connect, $row["post_id"]).'</button>


       <button type="button" class="btn btn-link like_button" data-post_id="'.$row["post_id"].'"><span class="glyphicon glyphicon-thumbs-up"></span> Like '.count_total_post_like($connect, $row["post_id"]).'</button>


       </p>
       <div id="comment_form'.$row["post_id"].'" style="display:none;">
        <span id="old_comment'.$row["post_id"].'"></span>
        <div class="form-group">
         <textarea name="comment" class="form-control" id="comment'.$row["post_id"].'"></textarea>
        </div>
        <div class="form-group" align="right">
         <button type="button" name="submit_comment" class="btn btn-primary btn-xs submit_comment">Comment</button>
        </div>
       </div>
      </div>
     </div>
    </div>
    ';
   }
  }
  else
  {
   $output = '<h4>No Post Found</h4>';
  }
  echo $output;
 }
 if($_POST['action'] == 'fetch_user')
 {
  $query = "
  SELECT * FROM tbl_twitter_user 
  WHERE user_id != '".$_SESSION["user_id"]."' 
  ORDER BY user_id DESC 
  LIMIT 15
  ";
  $statement = $connect->prepare($query);
  $statement->execute();
  $result = $statement->fetchAll();
  foreach($result as $row)
  {
   $profile_image = '';
   if($row['profile_image'] != '')
   {
    $profile_image = '<img src="images/'.$row["profile_image"].'" class="img-thumbnail img-responsive" />';
   }
   else
   {
    $profile_image = '<img src="images/user.jpg" class="img-thumbnail img-responsive" />';
   }
   $output .= '
   <div class="row">
    <div class="col-md-4">
     '.$profile_image.'
    </div>
    <div class="col-md-8">
     <h4><b>@'.$row["username"].'</b></h4>
     '.make_follow_button($connect, $row["user_id"], $_SESSION["user_id"]).'
     <span class="label label-success"> '.$row["follower_number"].' Followers</span>
    </div>
   </div>
   <hr />
   ';
  }
  echo $output;
 }

 if($_POST['action'] == 'follow')
 {
  $query = "
  INSERT INTO tbl_follow 
  (sender_id, receiver_id) 
  VALUES ('".$_POST["sender_id"]."', '".$_SESSION["user_id"]."')
  ";
  $statement = $connect->prepare($query);
  if($statement->execute())
  {
   $sub_query = "
   UPDATE tbl_twitter_user SET follower_number = follower_number + 1 WHERE user_id = '".$_POST["sender_id"]."'
   ";
   $statement = $connect->prepare($sub_query);
   $statement->execute();

   $notification_text = '<b>' . Get_user_name($connect, $_SESSION["user_id"]) . '</b> has follow you.';

   $insert_query = "
   INSERT INTO tbl_notification 
   (notification_receiver_id, notification_text, read_notification) 
   VALUES ('".$_POST["sender_id"]."', '".$notification_text."', 'no')
   ";

   $statement = $connect->prepare($insert_query);
   $statement->execute();
  }

 }

 if($_POST['action'] == 'unfollow')
 {
  $query = "
  DELETE FROM tbl_follow 
  WHERE sender_id = '".$_POST["sender_id"]."' 
  AND receiver_id = '".$_SESSION["user_id"]."'
  ";
  $statement = $connect->prepare($query);
  if($statement->execute())
  {
   $sub_query = "
   UPDATE tbl_twitter_user 
   SET follower_number = follower_number - 1 
   WHERE user_id = '".$_POST["sender_id"]."'
   ";
   $statement = $connect->prepare($sub_query);
   $statement->execute();

   $notification_text = '<b>' . Get_user_name($connect, $_SESSION["user_id"]) . '</b> has unfollow you.';

   $insert_query = "
   INSERT INTO tbl_notification 
   (notification_receiver_id, notification_text, read_notification) 
   VALUES ('".$_POST["sender_id"]."', '".$notification_text."', 'no')
   ";
   $statement = $connect->prepare($insert_query);

   $statement->execute();
  }
 }

 if($_POST["action"] == 'submit_comment')
 {
  $data = array(
   ':post_id'  => $_POST["post_id"],
   ':user_id'  => $_SESSION["user_id"],
   ':comment'  => $_POST["comment"],
   ':timestamp' => date("Y-m-d") . ' ' . date("H:i:s", STRTOTIME(date('h:i:sa')))
  );
  $query = "
  INSERT INTO tbl_comment 
  (post_id, user_id, comment, timestamp) 
  VALUES (:post_id, :user_id, :comment, :timestamp)
  ";
  $statement = $connect->prepare($query);
  $statement->execute($data);

  $notification_query = "
  SELECT user_id, post_content FROM tbl_samples_post 
  WHERE post_id = '".$_POST["post_id"]."'
  ";

  $statement = $connect->prepare($notification_query);

  $statement->execute();

  $notification_result = $statement->fetchAll();

  foreach($notification_result as $notification_row)
  {
   $notification_text = '<b>'.Get_user_name($connect, $_SESSION["user_id"]).'</b> has comment on your post - "'.strip_tags(substr($notification_row["post_content"], 0, 30)).'..."';

   $insert_query = "
   INSERT INTO tbl_notification 
   (notification_receiver_id, notification_text, read_notification) 
   VALUES ('".$notification_row['user_id']."', '".$notification_text."', 'no')
   ";

   $statement = $connect->prepare($insert_query);
   $statement->execute();
  }


 }
 if($_POST["action"] == "fetch_comment")
 {
  $query = "
  SELECT * FROM tbl_comment 
  INNER JOIN tbl_twitter_user 
  ON tbl_twitter_user.user_id = tbl_comment.user_id 
  WHERE post_id = '".$_POST["post_id"]."' 
  ORDER BY comment_id ASC
  ";
  $statement = $connect->prepare($query);
  $output = '';
  if($statement->execute())
  {
   $result = $statement->fetchAll();
   foreach($result as $row)
   {
    $profile_image = '';
    if($row['profile_image'] != '')
    {
     $profile_image = '<img src="images/'.$row["profile_image"].'" class="img-thumbnail img-responsive img-circle" />';
    }
    else
    {
     $profile_image = '<img src="images/user.jpg" class="img-thumbnail img-responsive img-circle" />';
    }
    $output .= '
    <div class="row">
     <div class="col-md-2">
     '.$profile_image.' 
     </div>
     <div class="col-md-10" style="margin-top:16px; padding-left:0">
      <small><b>@'.$row["username"].'</b><br />
      '.$row["comment"].'
      </small>
     </div>
    </div>
    <br />
    ';
   }
  }
  echo $output;
 }

 if($_POST['action'] == 'repost')
 {
  $query = "
  SELECT * FROM tbl_repost 
  WHERE post_id = '".$_POST["post_id"]."' 
  AND user_id = '".$_SESSION["user_id"]."'
  ";
  $statement = $connect->prepare($query);
  $statement->execute();
  $total_row = $statement->rowCount();
  if($total_row > 0)
  {
   echo 'You have already repost this post';
  }
  else
  {
   $query1 = "
   INSERT INTO tbl_repost 
   (post_id, user_id) 
   VALUES ('".$_POST["post_id"]."', '".$_SESSION["user_id"]."')
   ";
   $statement = $connect->prepare($query1);
   if($statement->execute())
   {
    $query2 = "
    SELECT * FROM tbl_samples_post 
    WHERE post_id = '".$_POST["post_id"]."'
    ";
    $statement = $connect->prepare($query2);
    if($statement->execute())
    {
     $result = $statement->fetchAll();
     $post_content = '';
     foreach($result as $row)
     {
      $post_content = $row['post_content'];
     }
     $query3 = "
     INSERT INTO tbl_samples_post 
     (user_id, post_content, post_datetime) 
     VALUES ('".$_SESSION["user_id"]."', '".$post_content."', '".date("Y-m-d") . ' ' . date("H:i:s", STRTOTIME(date('h:i:sa')))."')
     ";
     $statement = $connect->prepare($query3);
     if($statement->execute())
     {

      $notification_query = "
      SELECT user_id, post_content FROM tbl_samples_post 
      WHERE post_id = '".$_POST["post_id"]."'
      ";

      $statement = $connect->prepare($notification_query);

      $statement->execute();

      $notification_result = $statement->fetchAll();

      foreach($notification_result as $notification_row)
      {
       $notification_text = '<b>'.Get_user_name($connect, $_SESSION["user_id"]).'</b> has repost your post - "'.strip_tags(substr($notification_row["post_content"], 0, 30)).'..."';

       $insert_query = "
       INSERT INTO tbl_notification 
       (notification_receiver_id, notification_text, read_notification) 
       VALUES ('".$notification_row['user_id']."', '".$notification_text."', 'no')
       ";
       $statement = $connect->prepare($insert_query);
       $statement->execute();
      }

      echo 'Repost done successfully';
     }
    }
   }
  }
 }

 if($_POST["action"] == "like")
 {
  $query = "
  SELECT * FROM tbl_like 
  WHERE post_id = '".$_POST["post_id"]."' 
  AND user_id = '".$_SESSION["user_id"]."'
  ";
  $statement = $connect->prepare($query);
  $statement->execute();

  $total_row = $statement->rowCount();

  if($total_row > 0)
  {
   echo 'You have already like this post';
  }
  else
  {
   $insert_query = "
   INSERT INTO tbl_like 
   (user_id, post_id) 
   VALUES ('".$_SESSION["user_id"]."', '".$_POST["post_id"]."')
   ";

   $statement = $connect->prepare($insert_query);

   $statement->execute();

   $notification_query = "
   SELECT user_id, post_content FROM tbl_samples_post 
   WHERE post_id = '".$_POST["post_id"]."'
   ";

   $statement = $connect->prepare($notification_query);

   $statement->execute();

   $notification_result = $statement->fetchAll();

   foreach($notification_result as $notification_row)
   {
    $notification_text = '
    <b>' . Get_user_name($connect, $_SESSION["user_id"]) . '</b> has like your post - "'.strip_tags(substr($notification_row["post_content"], 0, 30)).'..."
    ';

    $insert_query = "
    INSERT INTO tbl_notification 
     (notification_receiver_id, notification_text, read_notification) 
     VALUES ('".$notification_row['user_id']."', '".$notification_text."', 'no')
    ";

    $statement = $connect->prepare($insert_query);
    $statement->execute();
   }

   echo 'Like';
  }

 }

 if($_POST["action"] == "like_user_list")
 {
  $query = "
  SELECT * FROM tbl_like 
  INNER JOIN tbl_twitter_user 
  ON tbl_twitter_user.user_id = tbl_like.user_id 
  WHERE tbl_like.post_id = '".$_POST["post_id"]."'
  ";

  $statement = $connect->prepare($query);

  $statement->execute();

  $result = $statement->fetchAll();

  foreach($result as $row)
  {
   $output .= '
   <p>'.$row["username"].'</p>
   ';
  }

  echo $output;
 }

 if($_POST["action"] == "fetch_link_content")
 {
  echo file_get_contents($_POST["url"][0]);
 }

 if($_POST["action"] == "update_notification_status")
 {
  $query = "
  UPDATE tbl_notification 
  SET read_notification = 'yes' 
  WHERE notification_receiver_id = '".$_SESSION["user_id"]."'
  ";

  $statement = $connect->prepare($query);

  $statement->execute();
 }

 if($_POST["action"] == "search_user")
 {
  $query = "
  SELECT username, profile_image FROM tbl_twitter_user 
  WHERE username LIKE '%".$_POST["query"]."%' 
  AND user_id != '".$_SESSION["user_id"]."'
  ";
  $statement = $connect->prepare($query);
  $statement->execute();
  $result = $statement->fetchAll();
  foreach($result as $row)
  {
   $data[] = $row["username"];
  }
  echo json_encode($data);
 }
}

?>


upload.php



?php

//upload.php

if(!empty($_FILES))
{
 $file_extension = strtolower(pathinfo($_FILES["uploadFile"]["name"], PATHINFO_EXTENSION));

 $new_file_name = rand() . '.' . $file_extension;

 $source_path = $_FILES["uploadFile"]["tmp_name"];

 $target_path = 'images/' . $_FILES["uploadFile"]["name"];

 if(move_uploaded_file($source_path, $target_path))
 {
  if($file_extension == 'jpg' || $file_extension == 'png')
  {
   echo '<p><img src="'.$target_path.'" class="img-responsive img-thumbnail" /></p><br />';
  }

  if($file_extension == 'mp4')
  {
   echo '
   <div class="embed-responsive embed-responsive-16by9">
    <video class="embed-responsive-item" controls="controls" src="'.$target_path.'"></video>
   </div>
   <br />
   ';
  }
 }
}

?>


wall.php



<!--
//wall.php
!-->

<?php

include('database_connection.php');

session_start();

if(!isset($_SESSION['user_id']))
{
 header('location:login.php');
}

$query = "
SELECT * FROM tbl_samples_post 
INNER JOIN tbl_twitter_user ON tbl_twitter_user.user_id = tbl_samples_post.user_id 
WHERE tbl_twitter_user.username = '".$_GET["data"]."' 
GROUP BY tbl_samples_post.post_id 
ORDER BY tbl_samples_post.post_id DESC
";

$statement = $connect->prepare($query);

$statement->execute();

$total_row = $statement->rowCount();

$user_id = Get_user_id($connect, $_GET["data"]);

?>

<html>  
    <head>  
        <title>Twitter Like Follow Unfollow System in PHP using Ajax jQuery</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.2/jquery.form.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"></script>
    </head>  
    <body>  
        <div class="container">
   <?php
   include('menu.php');
   ?>
   <div class="row">
    <?php
    if($total_row > 0)
    {
     $result = $statement->fetchAll();
    ?> 
    <div class="col-md-9">
     <div class="panel panel-default">
      <div class="panel-heading">
       <div class="row">
        <div class="col-md-6">
         <h3 class="panel-title"><?php echo '<b>'.$_GET["data"].'</b>';?> Post Details</h3>
        </div>
        <div class="col-md-6" align="right">
         <?php
         if($user_id != $_SESSION["user_id"])
         {
          echo make_follow_button($connect, $user_id, $_SESSION["user_id"]);
         }
         ?>
        </div>
       </div>
      </div>
      <div class="panel-body">
      <?php
      foreach($result as $row)
      {
       $profile_image = '';
       if($row['profile_image'] != '')
       {
        $profile_image = '<img src="images/'.$row["profile_image"].'" class="img-thumbnail img-responsive" />';
       }
       else
       {
        $profile_image = '<img src="images/user.jpg" class="img-thumbnail img-responsive" />';
       }

       $repost = 'disabled';

       if($row['user_id'] != $_SESSION['user_id'])
       {
        $repost = '';
       }

       echo '
       <div class="jumbotron" style="padding:24px 30px 24px 30px">
        <div class="row">
         <div class="col-md-2">
          '.$profile_image.'
         </div>
         <div class="col-md-10">
          <h3><b>@'.$row["username"].'</b></h3>
          <p>'.$row["post_content"].'<br /><br />
          <button type="button" class="btn btn-link post_comment" id="'.$row["post_id"].'" data-user_id="'.$row["user_id"].'">'.count_comment($connect, $row["post_id"]).' Comment</button>
          <button type="button" class="btn btn-danger repost" data-post_id="'.$row["post_id"].'" '.$repost.'><span class="glyphicon glyphicon-retweet"></span>&nbsp;&nbsp;'.count_retweet($connect, $row["post_id"]).'</button>
          <button type="button" class="btn btn-link like_button" data-post_id="'.$row["post_id"].'"><span class="glyphicon glyphicon-thumbs-up"></span> Like '.count_total_post_like($connect, $row["post_id"]).'</button>
          </p>
          <div id="comment_form'.$row["post_id"].'" style="display:none;">
           <span id="old_comment'.$row["post_id"].'"></span>
           <div class="form-group">
            <textarea name="comment" class="form-control" id="comment'.$row["post_id"].'"></textarea>
           </div>
           <div class="form-group" align="right">
            <button type="button" name="submit_comment" class="btn btn-primary btn-xs submit_comment" data-post_id="'.$row["post_id"].'">Comment</button>
           </div>
          </div>
         </div>
        </div>
       </div>
       ';
      }
      ?>
      </div>
     </div>
     
    </div>
    <div class="col-md-3">
     <div class="panel panel-default">
      <div class="panel-heading">
       <h3 class="panel-title"><?php echo $_GET["data"]; ?> Followers</h3>
      </div>
      <div class="panel-body">
      <?php

      $follower_query = "
      SELECT * FROM tbl_twitter_user 
       INNER JOIN tbl_follow 
       ON tbl_follow.receiver_id = tbl_twitter_user.user_id 
       WHERE tbl_follow.sender_id = '".$user_id."'
      ";

      $statement = $connect->prepare($follower_query);

      $statement->execute();

      $follower_result = $statement->fetchAll();

      foreach($follower_result as $follower_row)
      {
       $profile_image = '';
       if($follower_row['profile_image'] != '')
       {
        $profile_image = '<img src="images/'.$follower_row["profile_image"].'" class="img-thumbnail img-thumbnail" />';
       }
       else
       {
        $profile_image = '<img src="images/user.jpg" class="img-thumbnail img-responsive" />';
       }

       echo '
       <div class="row">
        <div class="col-md-4">
         '.$profile_image.'
        </div>
        <div class="col-md-8">
         <h4><b>@<a href="wall.php?data='.$follower_row["username"].'">'.$follower_row["username"].'</a></b></h4>
        </div>
       </div>
       <hr />
       ';
       
      }

      ?>
      </div>
     </div>
    </div>
    <?php
    }
    else
    {
     echo '<h3 align="center">No Post Found</h3>';
    }
    ?>
   </div>
  </div>
    </body>  
</html>

<?php
include('jquery.php');
?>


This is our PHP Ajax Follow Unfollow System source code, If there is any new feature has been added then we will add updated source code under this post.





27 comments:

  1. Thanks pls I need php code for students result check
    Email: raphaelugonna@gmail.com

    ReplyDelete
  2. Trending Now

    Notice: Array to string conversion in C:\xampp\htdocs\tutorial\Twitter Like Follow Unfollow System in PHP using Ajax jQuery\action.php on line 17
    No Post Found

    ReplyDelete
    Replies
    1. please, help, there's an error in the profile.php script, here are the errors it gives

      Notice: Array to string conversion in C:\Users\USER\Desktop\XAMP only\htdocs\followt\profile.php on line 79

      Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C:\Users\USER\Desktop\XAMP only\htdocs\followt\profile.php on line 79


      we have been complaining about this, please, admin help us fix this

      Delete
  3. Trending Now

    Notice: Array to string conversion in C:\xampp\htdocs\tutorial\Twitter Like Follow Unfollow System in PHP using Ajax jQuery\action.php on line 17
    No Post Found this is my problem what happend i do

    ReplyDelete
  4. SELECT * FROM tbl_samples_post
    LEFT JOIN tbl_twitter_user ON tbl_twitter_user.user_id = tbl_samples_post.user_id
    WHERE tbl_samples_post.user_id = '".$_SESSION["user_id"]."'
    GROUP BY tbl_samples_post.post_id
    ORDER BY post_id DESC";


    WHERE tbl_samples_post.user_id = '".$_SESSION["user_id"]."'

    I am getting errors in this field

    ReplyDelete
  5. Demo doesn't work. It'll register me, but it won't log me in.

    ReplyDelete
  6. Oh, okay. Nevermind. Login system works, just can't have spaces between first and last names. Thanks

    ReplyDelete
  7. Hi thank you for the lessons i wondering if you can add this tutorials how add multiple pictures and videos in post thanks for your time

    ReplyDelete
  8. Thanks for your tutorials, I would requesting you to add couple tutorials for image post and video post and profile system like Twitter all users can see your followers and folloing users

    ReplyDelete
  9. thanks sir this code is the best .you save me with the code.you is generious

    ReplyDelete
  10. the registration doesn't work

    ReplyDelete
  11. Warning: include(database_connection.php): failed to open stream: No such file or directory in /opt/lampp/htdocs/followers/register.php on line 7

    there is no file exist with this database_connection.php in your folder or if you already have made it then recheck your spellings

    ReplyDelete
  12. Please Update the downloading link

    ReplyDelete
  13. thank you, please add image view like your website picture view

    ReplyDelete
  14. please make tutorial gallery system have albums ( create album, delete album, rename album, getting albums size ) and image ( upload image, delete image, change images album to another album, rename image, getting size image upload image image with progress bar, image view like your websites image view ) thank you for your tutorials.

    ReplyDelete
  15. the script is ladden with errors, it doesn't work, i';m having errors in my profile.php script, it would not allow profile update s at all, please can anyone help me out? please

    ReplyDelete
  16. please, help, there's an error in the profile.php script, here are the errors it gives

    Notice: Array to string conversion in C:\Users\USER\Desktop\XAMP only\htdocs\followt\profile.php on line 79

    Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C:\Users\USER\Desktop\XAMP only\htdocs\followt\profile.php on line 79


    we have been complaining about this, please, admin help us fix this















    please, we've been begging help fix this
    my email is storyhub8@gmail.com

    feel free to send ud the correct profile.php script

    ReplyDelete
  17. what about repost option its not working

    ReplyDelete
  18. Has anyone been able to fix the issue on profile.php?

    ReplyDelete
  19. how to add pagination? please include this feature too. thank you.

    ReplyDelete
  20. Create post delete option for users.

    ReplyDelete
    Replies
    1. If you were able to make a code to delete posts I would really love for you to send me

      Delete
  21. A big Thanks to you sir, its really helpful

    ReplyDelete