CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   HTML & CSS (http://www.codingforums.com/forumdisplay.php?f=13)
-   -   Creating a youtube view countdown (http://www.codingforums.com/showthread.php?t=283967)

leew 12-11-2012 11:04 AM

Creating a youtube view countdown
 
Hi,

I want to create a very simple page that shows a countdown to a video getting a certain number of views.

At the moment I've been building it in wordpress, but ideally I'd like to build it as a html page. This is what I've done in wordpress so far:

I got this code to show the viewcount figure which worked:
<code>[ytviews video="DpwgblBeDz4"]</code>

then this one to try to show the number of views remaining until it gets to 220k which didn't:
<script type="text/javascript">
document.write((220000-(<code>[ytviews video="
DpwgblBeDz4"]</code>)));
</script>

Any help much appreciated! I'm hoping to get some code that will return the number value. Thanks,

tempz 12-11-2012 04:09 PM

You don't want it to show views until 220,000

I'm not sure that I fully understand you.

leew 12-12-2012 10:12 AM

I'd like to show the number of views to go until it reaches that target. So a calculation that is
=220,000 - (current viewcount on youtube server)

at the moment it would return 9077. Each time the page refreshes it would show the views still to go. It would be really cool if it could update in real time!

tempz 12-12-2012 02:44 PM

I think, the easiest way, is to get video info in JSON format. If you want to use JavaScript, try jQuery.getJSON()... But I prefer PHP:

Code:

<?php
$video_ID = 'your-video-ID';
$JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json");
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'};
echo $views;
?>


leew 12-13-2012 04:27 PM

Thanks - how do I put this into a html page? Sorry, I'm a novice

tempz 12-13-2012 06:44 PM

Here is an example:

http://dev.tempz.webatu.com/youtube-test/


PHP Code:


<div style="float:left;">
<iframe width="560" height="315" src="http://www.youtube.com/embed/Yx82V2zN3tQ" frameborder="0" allowfullscreen></iframe>

<p>
<div style="float:left; display:inline;">
Views:
<?php
$video_ID 
'Yx82V2zN3tQ'//http://www.youtube.com/watch?v=[Yx82V2zN3tQ]
$JSON file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json");
$JSON_Data json_decode($JSON);
$views $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'};
echo 
$views;
?>
</div>
</div>



All times are GMT +1. The time now is 03:28 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.