user123
user123

Reputation: 5407

Comment system: Showing post like facebook post section - (PHP Comment system 2)

How to show comment, like, share, time option as facebook?

I created a posts system that is similar to Facebook, it looks like the picture below [thanks to SO guys for this]:

My wall

I couldn't place the content properly. Where and how to adjust?

I want it to look similar to Facebook's posts section.

Facebook Wall

How to show comment, like, share option as above?

Code: index.php:

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){

$('#submit').on('click',function(e){
 e.preventDefault();
 input=$('#message').val();
 var comment={};
 comment.input=input;

 var commentdata=$("message").val();
  $.ajax({
        type: "POST",
                     data:{ 
            comment: commentdata
                },
        url: "ajax.php",
        success: function(data, textStatus){
            //alert(data);
            $("#commentsholder").append(data);
            $('#comments').append(comment.input);
            }
        },'html');
   });
});
</script>
</head>
<body>
<lable>Add post </lable><br>
<textarea id="message" rows="4" cols="50" placeholder="Add post"> </textarea>
<input type="submit" id="submit" value="share"> </input>
<div>
<div id="commentsholder"></div>
<ul id="comments" ></ul>
</div>
</body>
</html>

ajax.php:

<?php
$comment=$_POST['commentdata'];

$con = mysqli_connect('127.0.0.1:3306', 'root', 'root', 'test');
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$query= "select * from user_record where id=100005809179068";

$result = mysqli_query($con,$query);


//build html format as you need it..
while($row = mysqli_fetch_array($result)){

echo '<div class="comment"><img src="'.$row ['picture'].'"/>'.$row ['name'].'</div>';
}
?>

Upvotes: 0

Views: 5434

Answers (2)

karmiphuc
karmiphuc

Reputation: 81

1. First, why don't the post and the comment show properly?!

You should change the ajax call into this:

$.ajax({
    type: "POST",
    data:{ 
        comment: commentdata,
        input: comment.input // Add your input as a parameter
    },
    url: "ajax.php",
    success: function(data, textStatus){
        $("#commentsholder").append(data);
    }
 },'html');

Your ajax.php:

$input = $_POST['input'];
echo '<div class="comment"><img src="'.$row ['picture'].'"/>'.$row ['name'].
'<p>'.$input.'</p></div>';

It should work now. Your avatar will always stay in the same block with your comment.

2. Second, add Like | Comment | Share. You could do this in your server-side ajax or client-side like below (I reuse the success callback):

success: function(data, textStatus){
                $("#commentsholder").append(data); // This should append the div.comment
                $('.comment').append(comment.input); // Not $('#comments')
                var likeLink = '<a href="http://www.facebook.com/plugins/like.php?href=[YOUR_POST_URL]&width=100&height=80&colorscheme=light&layout=standard&action=like&show_faces=true&send=false&appId=[YOUR_APPID]">Like</a>';
                var shareLink = '<a title="send to Facebook" href="http://www.facebook.com/sharer.php?u=YOUR_URL" target="_blank">Share</a>';
                $('.comment').append(likeLink).append(shareLink);
                }

You could use the fiddle here: http://jsfiddle.net/karmiphuc/SDSwk/

About the post comments, you have to use FB Graph API to send and retrieve them.

Hope this helps.

Upvotes: 2

Ali Alnoaimi
Ali Alnoaimi

Reputation: 2317

Add it to this section of your code

echo '<div class="comment"><img src="'.$row ['picture'].'"/>'.$row ['name'].'</div><br />Comment | Share | etc...';

Upvotes: 1

Related Questions