Click to See Complete Forum and Search --> : Random Video


SarahBab
09-09-2007, 01:04 AM
Hi all.

My first post, (be kind!) been reading forums for a while tho'.

I have been using DivX web player recently and luv it.

What I'd like is to dump some numbered video clips into a folder and (without using a database) have PHP select one at random, then using a variable - plug the addy in to the DivX plugin code.

Hope this makes sense.

my files are video.divx, video2.divx and so forth - the folder is named "video".

I would use an include file - I got those working tonight! :)

Im new to PHP so my programming skills are basic. Playing DivX works fine but that is just with the one clip. Could use some advice.

Thank-you!

NogDog
09-09-2007, 02:44 AM
You could use glob (http://www.php.net/glob)() to get an array of files, then array_rand (http://www.php.net/array_rand)() to randomize that array. Then just pick the first or last one off the array with array_shift() or array_pop().

bokeh
09-09-2007, 06:13 AM
You could use glob (http://www.php.net/glob)() to get an array of files, then array_rand (http://www.php.net/array_rand)() to randomize that array. Then just pick the first or last one off the array with array_shift() or array_pop().array_rand (http://www.php.net/array_rand)() does not randomize an array (use shuffle (http://www.php.net/shuffle) to do that), it returns a random key (or array of keys if the second argument is used).$glob = glob(/* args */);
$RandomFile = $glob[array_rand($glob)];

NogDog
09-09-2007, 03:46 PM
array_rand (http://www.php.net/array_rand)() does not randomize an array (use shuffle (http://www.php.net/shuffle) to do that), it returns a random key (or array of keys if the second argument is used).$glob = glob(/* args */);
$RandomFile = $glob[array_rand($glob)];
That's what I get for going by memory instead of double-checking. At least my memory was correct in that there is an array_rand() function, even if I confused what its actual functionality is. :p

SarahBab
09-10-2007, 07:25 AM
I ended up doing it without an array, direct on the page.


<?php
// remove a single variable
unset($random);
srand(time());
$random = (rand()%6);
?>

and then later in the code
<embed type="video/divx" src="multimedia/movies/<?php echo "$random"; ?>.divx"

I used the same variable to load the video, the thumbnail and a nice description too.

--

Seems to work fine.
Thanks for the help.