Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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,

  • #2
    Regular Coder
    Join Date
    Jul 2012
    Location
    London
    Posts
    473
    Thanks
    4
    Thanked 86 Times in 86 Posts
    You don't want it to show views until 220,000

    I'm not sure that I fully understand you.

  • #3
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!

  • #4
    Regular Coder
    Join Date
    Jul 2012
    Location
    London
    Posts
    473
    Thanks
    4
    Thanked 86 Times in 86 Posts
    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;
    ?>

  • Users who have thanked tempz for this post:

    leew (12-13-2012)

  • #5
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks - how do I put this into a html page? Sorry, I'm a novice

  • #6
    Regular Coder
    Join Date
    Jul 2012
    Location
    London
    Posts
    473
    Thanks
    4
    Thanked 86 Times in 86 Posts
    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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •