1

I'm trying to display Facebook comment counts in <div id="comments">

It has to be via Facebook Query Language (FQL). This post is almost exactly what I need: Facebook Graph Api url comments and shares count doesn't work anymore

But how do I display the comment_count (from the query) into a div? i.e. how do I process that data? So far, I have:


$(function(){
 $.ajax({
  url: 'https://graph.facebook.com/fql?q=SELECT%20comment_count%20FROM%20link_stat%20WHERE%20url=%27e',
  dataType: 'jsonp',
  success: function(data) {
   if(data.comment_count)
   {
    $('body').find('#comments').html('Comments ('+jsonp.data.comment_count+')');
   }else{
    $('body').find('#comments').html('Comments (0)');
   }
  }
 });
});
Community
  • 1
  • 1
melusinefile
  • 13
  • 1
  • 1
  • 5
  • Hi! - the url is just an example: a complete url would be, for instance: https://graph.facebook.com/fql?q=SELECT%20comment_count%20FROM%20link_stat%20WHERE%20url=%27google.com%27 – melusinefile Jan 17 '13 at 06:48

2 Answers2

1

I did it like this to update my div with the likes count like this

$fql  = "SELECT url, normalized_url, share_count, like_count, comment_count, ";
    $fql .= "total_count, commentsbox_count, comments_fbid, click_count FROM ";
    $fql .= "link_stat WHERE url = '".$url."'";

$j.ajax({
            url: 'https://api.facebook.com/method/fql.query?format=json&query=<?php echo urlencode($fql);?>',
            dataType: 'jsonp',
            success: function(data) 
            {
$j(".comment_count").html(data.comment_count);
}
});

works for me like a charm.

  • so you're saying all I need to do is replace -- url = '".$url."'"; with my url? e.g. (url='http://google.com')? (Tried that, it didn't work. Thank you! – melusinefile Jan 17 '13 at 07:37
  • basically, to clarify, I need to display in a div the comment_count on this example link: https://graph.facebook.com/fql?q=SELECT%20comment_count%20FROM%20link_stat%20WHERE%20url=%27google.com%27 – melusinefile Jan 17 '13 at 11:31
  • The .comment_count is the class of the div and it will replace the html of div with the comment count. –  Feb 07 '13 at 07:59
1

For my part,

I used php code to get the comment count via fql. First, you need to download the facebook php sdk and load it at the top of your page:

require_once("src/facebook.php");

  $config = array(
    'appId' => 'YOUR_APP_ID',
    'secret' => 'YOUR_SECRET_KEY',
  );

  $facebook = new Facebook($config);

Then, the fql query:

$url  = 'http://www.yoururl.com/;

$fquery = 'SELECT comment_count, share_count, like_count FROM link_stat WHERE url = "'.$url.'"';
$fparam = array( 'method' => 'fql.query', 'query' => $fquery );
$fql = $facebook->api($fparam);

$cmcount = $fql[0]['comment_count'];

So, $cmcount is now your comment counts, put it directly in your html code:

<div id="comments">
<?php echo $cmcount; ?>
</div>
pmrotule
  • 9,065
  • 4
  • 50
  • 58