Note that there are some explanatory texts on larger screens.

plurals
  1. POFacebook Like Count Auto Refresh
    primarykey
    data
    text
    <p>I am new to working with the Facebook API and I have a question that I believe should be simple.</p> <p>I am setting up a TV screen for a store that will display a webpage. On the page I am going to display the current 'like count' of the store's facebook page. I know how to set it up to display the likes but is there an easy way to have the number auto-refresh when new people like the page WITHOUT refreshing the whole page. I guess I'm looking for it to be generated dynamically. I'm not sure how to go about this. I'm willing to do the research, I just need to be pointed in the right direction.</p> <p>Here's the code in the body of the page so far:</p> <pre><code>&lt;?php function fan_count($fan_ID) { $info = json_decode(file_get_contents('http://graph.facebook.com/' . $fan_ID)); echo $info-&gt;likes; } function fan_name($fan_ID) { $info = json_decode(file_get_contents('http://graph.facebook.com/' . $fan_ID)); echo $info-&gt;name; } ?&gt; &lt;div style="width:100px; height:100px; border:1px solid black;"&gt; Facebook Logo &lt;/div&gt; &lt;div class="fb_stats" id="fb_name"&gt; &lt;?php fan_name(PAGE_ID)?&gt; &lt;/div&gt; &lt;div class="fb_stats" id="fb_likes"&gt; &lt;?php fan_count(PAGE_ID)?&gt; &lt;/div&gt; </code></pre> <p>Thanks for any help you can provide!</p> <h2>EDIT</h2> <p>Alright, here is my updated code per the answer below. I can't get the PHP on the main page to work (I'll worry about the reloading once I have this bit set up correctly). I'm just looking to grab the like count for simplicities sake and I'll build from there. Here's my code (I'm using 'cocacola' as an example; vanity URLs are compatible with the Facebook graph btw):</p> <pre><code>&lt;?php class fanpage { public $fan_ID; public $info; function __construct($fan_ID) { $this-&gt;fan_ID = $fan_ID; $this-&gt;info = json_decode(file_get_contents('http://graph.facebook.com/' . $fan_ID)); } function fan_count() { echo $info-&gt;likes; } } $fp = new fanpage(cocacola); ?&gt; &lt;div style="width:100px; height:100px; border:1px solid black;"&gt; Facebook Logo &lt;/div&gt; &lt;div class="fb_stats" id="fb_likes"&gt; &lt;?php $fp-&gt;fan_count()?&gt; &lt;span style="font-size:20px; position:absolute; top:400px;"&gt;Total Likes&lt;/span&gt; &lt;/div&gt; </code></pre> <p>Does it possibly have anything to do with $info being defined in one function and trying to be called in another? Thanks for the help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload