Well here is another random function from my little repository.. A little while back one of my clients presented me with the idea that they wanted to display a set of videos from there YouTube gallery at random on there home page and various other sections of there site. But only specific videos did they want. So from that I created a little function. That would do just that. I will say though this function is a bit toned down from what I actually gave them as they wanted a small control panel to go with it ran off a mysql database so they could add and remove videos from from randomly chosen videos they provided from there YouTube gallery they also wanted to provide a brief description on there page for each one as well amongst a couple other things with it. Which I may re-tailor that version of the script eventually and release it to the public as I think its a nice idea. Could make for a good 404 error page or like with them a good way to keep the content on there main page changing but also display some of there portfolio of videos at the same time.
Anyway back to the meat of this article I doubt you want to read more than you have to and lets face it, if you’ve seen my other articles here you would know writing isn’t really my forte. That said heres the code and a brief explanation and as always a demo of it working.
function randomTube(){
$RVideoArray = array('q8SWMAQYQf0', 'q8SWMAQYQf0', 'q8SWMAQYQf0', 'hpxxvOU3vbI');
$RVideoCount = count($RVideoArray);
$RVideoSelect = rand(0, $RVideoCount - 1);
$RVideoNum = trim($RVideoSelect);
$RVideo = $RVideoArray[$RVideoNum];
$theTube = '<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/'.$RVideo.'"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'.$RVideo.'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>';
return $theTube;
}
In this version of the script, I have set up an array that you can add/remove you’re choice of YouTube videos from. All you need to do is obtain the YouTube short code for the video or what ever it is they call it specificly, I call it short code cause to me it resembles a familiar look/feel to that of a short URL through something like bit.ly. But thats off topic. With almost any youtube video there is always a URL construct similar to www.youtube.com/watch?v= what follows the v= and in some cases comes before the next & is the actual bit of the URL you are looking for to use with the array $RVideoArray in the function above.
Example of usage for the Random YouTube video function is.
$myRandomVideo = randomTube(); echo $myRandomVideo;
