Jump to content

Youtube php embed help. Noob Alert!


CEinNYC

Recommended Posts

Hey all -

 

Below is my code.  I'm uncertain how to move correctly code (syntax) what I'm looking to do..

 

When the user clicks "Next" or "Previous," the following youtube clip will load into the div tag.  (With the corresponding description tag).  Where do I incorporate the str_replace?  And how do I make sure both the youtube clip with the correct description is loaded?

 

Any advice is appreciated!

 

<?php

$youtubeIds = array("rHNtuaVHenM","zIAdbIMNq9g","QQ4TlJLa-n4","JccPOA12ELI","JccPOA12ELI", "TWhhXVqVB7E");

$embedCode = '<object width="853" height="505"><param name="movie" value="http://www.youtube.com/watch?v=__YOUTUBE_ID__?fs=1&hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/watch?v=__YOUTUBE_ID__?fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="853" height="505"></embed></object>';

 

 

 

if(isset($_GET['youtubeclip']))

{

if(is_numeric($_GET['youtubeclip']))

{

if($_GET['youtubeclip'] >= 0 && $_GET['youtubeclip'] <= 5)

{

$youtubeclip = $_GET['youtubeclip'];

}

 

else

{

$youtubeclip = 0;

}

}

else

{

$youtubeclip = 0;

}

 

$prevyoutubeclip = $youtubeclip-1;

if($prevyoutubeclip < 0)

{

$prevyoutubeclip = 5;

}

 

$nextyoutubeclip = $youtubeclip+1;

if($nextyoutubeclip > 5)

{

$nextyoutubeclib = 0;

}

 

$clip_names= array('Episode #1', 'Episode #2', 'Episode #3', 'Episode #4', 'Episode #5', 'Episode #6');

}

 

 

 

?>

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

 

<title>Billy Green</title>

 

<link rel="stylesheet" type="text/css" media="screen" href="episode_style.css">

 

</head>

 

<body>

 

<div class="wrapper">

<?php include 'header.php'?>

 

<div id="episode_content">

 

 

<div class="youtube_box">

<?php

 

echo $embedCode;

 

?>

 

</div>

 

 

<h2 class="clip-description">"<?php echo $clip_names[$youtubeclip]; ?>" </h2>

<div class="youtube_control">

<a href="episodes.php?youtubeclip=<?php echo $prevyoutubeclip; ?>"> Previous Clip </a>

<a href="episodes.php?youtubeclip=<?php echo $nextyoutubeclip; ?>"> Next Clip </a>

</div>

 

 

</div>

<div class="clearIt"> </div>

 

<?php include 'footer.php'?>

</div>

 

 

 

 

 

 

</body>

</html>

 

 

 

 

 

Link to comment
Share on other sites

Ew that is messy... :D First, when entering html into a PHP variable, you need to escape all " with a /.

And in forums, use the PHP button (above) when pasting code so we can read it better.

 

Sooooo....

 

$embedCode = '<object width="853" height="505"><param name="movie" value="http://www.youtube.com/watch?v=__YOUTUBE_ID__?fs=1&hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/watch?v=__YOUTUBE_ID__?fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="853" height="505"></embed></object>';

 

Would be....

 

$embedCode = "<object width=/"853/" height=/"505/"><param name=/"movie/" value=/"http://www.youtube.com/watch?v=__YOUTUBE_ID__?fs=1&hl=en_US/"></param><param name=/"allowFullScreen/" value=/"true/"></param><param name=/"allowscriptaccess/" value=/"always/"></param><embed src=/"http://www.youtube.com/watch?v=__YOUTUBE_ID__?fs=1&hl=en_US/" type=/"application/x-shockwave-flash/" allowscriptaccess=/"always/" allowfullscreen=/"true/" width=/"853/" height=/"505/"></embed></object>";

Link to comment
Share on other sites

Okay, will do regarding appropriate syntax.  (I should read the rules!)

Thanks for of this...

 

Sooooo..  How do I incorporate the youtube clip, into the emded code, and have the operation work with the "next" and "previous" buttons?  (With the corresponding description tag).  I'm sure this is a moronic question regarding accessing arrays but, it's where I am right now. :)

 

Thanks again!

Link to comment
Share on other sites

So basically you did not set your array in the header your code is messed up. I will attempt to change it later I am on my blackberry right now but here is some of it corrected see if it works.

 

<?php
   $youtubeIds = array("rHNtuaVHenM","zIAdbIMNq9g","QQ4TlJLa-n4","JccPOA12ELI","JccPOA12ELI", "TWhhXVqVB7E");
   $embedCode = '<object width="853" height="505"><param name="movie" value="http://www.youtube.com/v/' . $youtubeIds['$youtubeclip'] .'?fs=1&hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' . $youtubeIds['$youtubeclip'] .'?fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="853" height="505"></embed></object>';

   
   
   if(isset($_GET['youtubeclip']))
   {
   if(is_numeric($_GET['youtubeclip']))
      {
         if($_GET['youtubeclip'] >= 0 && $_GET['youtubeclip'] <= 5)
         {
            $youtubeclip = $_GET['youtubeclip'];
         }
         
         else
         {
            $youtubeclip = 0;
         }      
      }
   else
   {
      $youtubeclip = 0;
   }
   
   $prevyoutubeclip = $youtubeclip-1;
   if($prevyoutubeclip < 0)
   {
      $prevyoutubeclip = 5;
   }
   
   $nextyoutubeclip = $youtubeclip+1;
   if($nextyoutubeclip > 5)
   {
      $nextyoutubeclib = 0;
   }
   
   $clip_names= array('Episode #1', 'Episode #2', 'Episode #3', 'Episode #4', 'Episode #5', 'Episode #6');
   }



?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

   <title>Billy Green</title>
   
   <link rel="stylesheet" type="text/css" media="screen" href="episode_style.css">
   
</head>

<body>
   
   <div class="wrapper">
      <?php include 'header.php'?>
      
      <div id="episode_content">
      
         
         <div class="youtube_box">
         <?php
      
         echo $embedCode;
         
         ?>
         
         </div>
      
         
         <h2 class="clip-description">"<?php echo $clip_names[$youtubeclip]; ?>" </h2>
         <div class="youtube_control">
         <a href="episodes.php?youtubeclip=<?php echo $prevyoutubeclip; ?>"> Previous Clip </a>
         <a href="episodes.php?youtubeclip=<?php echo $nextyoutubeclip; ?>"> Next Clip </a>
         </div>

         
      </div>   
      <div class="clearIt"> </div>   
   
      <?php include 'footer.php'?>
   </div>
   
   




</body>
</html>

 

Tell me what happens.

Thanks,

Colton Wagner

Link to comment
Share on other sites

There are 2 methods that I can think of for this.

1 - Use JavaScript to change the youtube URL's - No page reload

2 - Use PHP with a GET variable from the URL - Requires page reload

 

If you tell me what sort of thing your looking for I'm sure I could come up with something.

 

I have made something like this in the past, I used a mix of both to get a good result.

My version also automatically moved to the next song/video when the video had finished and would loop if it had come to the end of the array of songs/videos.

 

Regards, Paul.

 

Link to comment
Share on other sites

Thanks for this.. !  Below is the link to the site (in progress).   

 

The box on the left has an embedded youtube object code.  As the user clicks "next" or "previous" the corresponding video loads.  The youtubeIds are in an array, and I assume that I need to use "str_replace," however I am unable to connect the "next/previous" buttons, with both the video description and the youtubeId array.

 

Because of the restraints of the assignment, I need to keep it solely to php.

 

Thanks for any advice!  I assume there's a small php function that will solve all my problems (in both life and this assignment).

 

Please excuse the sloppy site.  It's in progress.

 

Link: http://a.parsons.edu/~eckec706/temp/episodes.php

(you may need to click "next" to pull up the first video..  Another issue, but I'll leave that for another time).

 

Code attached.

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

So it has to be done in solely PHP, well that should be quite simple to do in theory.

Are the arrays hard-coded or are they dynamic, as in change depending on a variable.

 

If you give me the array you are working with I will muster something up for you :)

 

Regards, Paul.

Link to comment
Share on other sites

Thanks!  I'll have to pay this forward when I'm a php wiz.  8)

 

 

Here's the php..  Note "__YOUTUBE_ID__" is to indicate where to place the .str_replace.

 

At the end of the php is the html function.

 

<?php

$youtubeIds = array("rHNtuaVHenM","zIAdbIMNq9g","QQ4TlJLa-n4","JccPOA12ELI","JccPOA12ELI", "TWhhXVqVB7E");

$embedCode = '<object width="640" height="385"><param name="movie" value="http://www.youtube.com/watch?v=__YOUTUBE_ID__?fs=1&hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/watch?v=__YOUTUBE_ID__?fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>';

 

 

if(isset($_GET['youtubeclip']))

{

if(is_numeric($_GET['youtubeclip']))

{

if($_GET['youtubeclip'] >= 0 && $_GET['youtubeclip'] <= 5)

{

$youtubeclip = $_GET['youtubeclip'];

}

 

else

{

$youtubeclip = 0;

}

}

else

{

$youtubeclip = 0;

}

 

$prevyoutubeclip = $youtubeclip-1;

if($prevyoutubeclip < 0)

{

$prevyoutubeclip = 5;

}

 

$nextyoutubeclip = $youtubeclip+1;

if($nextyoutubeclip > 5)

{

$nextyoutubeclib = 0;

}

 

$clip_names= array('Episode #1', 'Episode #2', 'Episode #3', 'Episode #4', 'Episode #5', 'Episode #6');

}

?>

 

------------------------------------

 

<div class="youtube_box">

<?php echo $embedCode ?>

 

<div class="clip-description"><h2> <?php echo $clip_names[$youtubeclip]; ?> </h2></div>

 

<div class="youtube_buttons_previous"><a href="episodes.php?youtubeclip=<?php echo $prevyoutubeclip; ?>"><img src="img/main/previous.png" border="0" width="184" height="81" alt="previous"/></a></div>

<div class="youtube_buttons_next"><a href="episodes.php?youtubeclip=<?php echo $nextyoutubeclip; ?>"><img src="img/main/next.png" border="0" width="101" height="79" alt="next"/></a></div>

</div>

<div class="bgphoto"><img src="img/video/bg.png" width="" height="" alt="Billy Green"/>

</div>

 

 

Link to comment
Share on other sites

I have just written the following code for you to use, I have supplied sufficent information and comments.

Now it's up to you to use the script accordingly :)

 

<?PHP

  // Function to retrieve data from video information file.
  function getData($string,$oTag,$cTag) {
    $start = strpos($string, $oTag);
    $end   = strpos($string, $cTag, ($start + strlen($oTag)));
    $start = ($start + strlen($oTag));
    $end   = ($end - $start);

    return substr($string, $start, $end);
  }

  // Function to get and set information about the current episode
  function currentEpisode($youtubeURL) {
    $stats = 'http://gdata.youtube.com/feeds/api/videos/'.$youtubeURL;
    $stats = file_get_contents($stats);

    $videoURL = 'http://www.youtube.com/v/'.$youtubeURL;
    $videoDes = getData($stats,"<media:description type='plain'>","</media:description>");
    $videoTtl = getData($stats,"<title type='text'>","</title>");

    $information = array("title"=>$videoTtl, "description"=>$videoDes, "URL"=>$videoURL);
    return $information;
  }

  // Function to set the next link
  function nextLink($num,$count) {
    if($num == $count) {
      $nextLink = 1;
    } else if($num+1 > $count) {
      $nextLink = 1;
    } else {
      $nextLink = $num+1;
    }
    return $nextLink;
  }

  // Function to set the previous link
  function prevLink($num) {
    if($num-1 <= 1) {
      $prevLink = 1;
    } else {
      $prevLink = $num-1;
    }
    return $prevLink;
  }

  // Place episodes in an array
  $episodes = array("1"=>"rHNtuaVHenM","zIAdbIMNq9g","QQ4TlJLa-n4","JccPOA12ELI","JccPOA12ELI", "TWhhXVqVB7E");

  // Count the number of episode
  $countEpisodes = count($episodes);

    // If $_GET['episode'] is a number we'll set the variable, otherwise set as first episode.
    if(is_numeric($_GET['episode'])) {
      $currentEpisode = $_GET['episode'];
    } else {
      $currentEpisode = 1;
    }

    // If the current episode number is greater than episode count, set episode as last.
    if($currentEpisode > $countEpisodes) {
      $currentEpisode = $countEpisodes;
    }

    // If the current episode mumber is less than 1, set the episode as 1
    if($currentEpisode < 1) {
      $currentEpisode = 1;
    }

  $nextLink = '<input onClick="window.location=\'?episode='. nextLink($currentEpisode,$countEpisodes) .'\';" type="button" name="next" value=">>">';
  $prevLink = '<input onClick="window.location=\'?episode='. prevLink($currentEpisode)                .'\';" type="button" name="prev" value="<<">';

  // Get the information for current episode selected
  $thisEpisode = currentEpisode($episodes[$currentEpisode]);
  // Set the episode URL for embed
  $thisURL = $thisEpisode['URL'];
  // Set the episode description
  $thisDes = nl2br($thisEpisode['description']);
  // Set the episode title
  $thisTtl = $thisEpisode['title'];
  
  echo '<pre>';
  print_r($thisEpisode);
  echo '</pre>';

  echo $prevLink;
  echo $nextLink;
?>

 

Tell me how you get on with it.

 

Regards, Paul.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.