Dynadot โ€” .com Registration $8.99

Displaying a random youtube video from a list

Spaceship Spaceship
Watch

dan_Vt

Established Member
Impact
13
I've done some searching and can't find this. I'd like a php script that will display a random youtube video picked from a list I create (at a resolution I define).

Anyone know of such a thing?
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
If you can't find one, then you will need to make one youself.
If you are creating the list yourself, then you can do a SELECT from your database and use rand() with php or Mysql
Code:
SELECT * FROM table1, table2 WHERE a=b AND c<d -> ORDER BY RAND() LIMIT 1000;
 
0
•••
Thanks for the info. I don't have a database, I was hoping to have a php script that would have the youtube code then pull the video id from a random line of a txt file. Possible?
 
0
•••
PHP:
<?php

$file = "path/to/text/file.txt";

$data = file($file);
$line = trim($data[rand(0,count($data))]);
echo '<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/'.$line.'"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'.$line.'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>';

?>
 
0
•••
Dan said:
PHP:
<?php

$file = "path/to/text/file.txt";

$data = file($file);
$line = trim($data[rand(0,count($data))]);
echo '<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/'.$line.'"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'.$line.'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>';

?>

Oh man! That's working but sometimes it doesn't load anything (almost like there's a blank line, which there isn't). NPs, rep, nomination for member of the month if you can figure that one out!
 
0
•••
Does it have the <object... code when nothing appears or does absolutely nothing show up?
 
0
•••
Dan said:
Does it have the <object... code when nothing appears or does absolutely nothing show up?
nothing shows at all when that happens...

it won't let me give you more rep :(
 
0
•••
$line = trim($data[rand(0,count($data))]); <-- It shows whichever video is in position 0.

If you changed that variable to 3, then it would show position 3 (i.e. the 4th URL).

At least, that's what happeneding on my server.
 
0
•••
Try array_rand:

PHP:
<?php

$file = 'path/to/text/file.txt';

$data = file($file);
$line = trim($data[array_rand($data)]);

echo <<<YT
<object width="425" height="350">
	<param name="movie" value="http://www.youtube.com/v/$line"></param>
	<param name="wmode" value="transparent"></param>
	<embed src="http://www.youtube.com/v/$line" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed>
</object>
YT;

?>
 
0
•••
SecondVersion said:
Try array_rand:

PHP:
<?php

$file = 'path/to/text/file.txt';

$data = file($file);
$line = trim($data[array_rand($data)]);

echo <<<YT
<object width="425" height="350">
	<param name="movie" value="http://www.youtube.com/v/$line"></param>
	<param name="wmode" value="transparent"></param>
	<embed src="http://www.youtube.com/v/$line" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed>
</object>
YT;

?>

That seems to fix the issue I had with it occasionally showing nothing.

Thanks, won't let me send rep again, so NPs on the way.
 
0
•••
No problem. :)
 
0
•••
That's very interesting. Will try it sometimes. :)
 
0
•••
SecondVersion said:
Try array_rand:
Do you know why that worked or why my version randomly didn't work?
 
0
•••
Dan said:
Do you know why that worked or why my version randomly didn't work?
Arrays start at key 0, so when you:
PHP:
$line = trim($data[rand(0,count($data))]);
If rand returns the total #, there won't be an entry in the array for it.

You'd have to:
PHP:
$line = trim($data[rand(0,count($data)-1)]);

With array_rand, you don't have to make that adjustment. array_rand is the preferred method for getting random entires from an array anyway.
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back