// check folder:
if (!is_dir($folder)){ die('invalid folder'); }
// create files array
$files = array();
// open directory
if ($dir = @opendir($folder)){
// go trough all files:
while($file = readdir($dir)){
if (!preg_match('/^\.+$/', $file) and
preg_match('/\.('.$extensions.')$/', $file)){
// feed the array:
$files[] = $file;
}
}
// close directory
closedir($dir);
}
else {
die('Could not open folder "'.$folder.'"');
}
if (count($files) == 0){
die('No files where found :-(');
}
// seed random function:
mt_srand((double)microtime()*1000000);
// get an random index:
$rand = mt_rand(0, count($files)-1);
// check again:
if (!isset($files[$rand])){
die('Array index not found');
}
// return the random file:
return $folder . "/" . $files[$rand];
}
$random1 = RandomFile("random");
while (!$random2 || $random2 == $random1) {
$random2 = RandomFile("random");
}
while (!$random3 || $random3 == $random1 || $random3 == $random2) {
$random3 = RandomFile("random");
}
while (!$random4 || $random4 == $random1 || $random4 == $random2 ||
$random4 == $random3) {
$random4 = RandomFile("random");
}
while (!$random5 || $random5 == $random1 || $random5 == $random2 ||
$random5 == $random3) || $random5 == $random4) {
$random5 = RandomFile("random");
}
while (!$random6 || $random6 == $random1 || $random6 == $random2 ||
$random6 == $random3) || $random6 == $random4) || $random6 == $random5)
{
$random6 = RandomFile("random");
}
while (!$random7 || $random7 == $random1 || $random7 == $random2 ||
$random7 == $random3) || $random7 == $random4) || $random7 ==
$random5) || $random7 == $random6){
$random7 = RandomFile("random");
}
?>
You probably want to use shuffle() instead of mt_rand() and an array
instead of seven $randomX variables. And glob() is handy as well.
--
-- http://alvaro.es - �lvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programaci�n web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Shuffle?? Ooops, it seems that my extremely old and weak PHP-knowledge
have som deep, black holes... Now I feel more stuck than ever...
I am confused. You say that you want the output to be sorted, but you
seem to be writing a script to make the output random (i.e. not
sorted).
If you want it sorted, what criteria do you want to sort it on?
I have reorganized the script and have the images sorted in an array,
like this:
$images = array("1" =>"imageone.png",
"2" =>"imagetwo.png",
"3" =>"imagethree.png" ... etc..
then I want to random pick 4 image values:
$rand_keys = array_rand($images, 4);
and sort these according to their keys.
You've got the hard part. Now use sort() to sort they keys into
ascending order, then get the appropriate elements from $images based on
the keys in $rand_keys, i.e.
sort($rand_keys);
foreach ($rand_keys as $key)
echo $images[$key] . "<br>\n"; // Or whatever you want
Alternatively:
for ($i = 0; $i < count($rand_keys); $i++)
echo $images[$rand_keys[$i]] . "<br>\n"; // Again whatever you want
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================
>
> Shuffle?? Ooops, it seems that my extremely old and weak PHP-knowledge
> have som deep, black holes... Now I feel more stuck than ever...
According to the manual, shuffle() was introduced in PHP 4 so it's like
ten years old ;-)
Never mind, I see from the other subthread that I didn't fully
understand your requirements and you already got the answer.
--
-- http://alvaro.es - �lvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programaci�n web: http://borrame.com
>I have reorganized the script and have the images sorted in an array,
>like this:
>
>
>
>$images = array("1" =>"imageone.png",
> "2" =>"imagetwo.png",
> "3" =>"imagethree.png" ... etc..
>
>then I want to random pick 4 image values:
>$rand_keys = array_rand($images, 4);
>
>and sort these according to their keys.
<?php
$images = array(
1 => "image1.png", 2 => "image2.png", 3 => "image3.png",
4 => "image4.png", 5 => "image5.png", 6 => "image6.png",
7 => "image7.png", 8 => "image8.png", 9 => "image9.png",
);
$rand_keys = array_rand($images, 4);
sort($rand_keys);
$result = array_intersect_key($images, array_flip($rand_keys));
var_dump($result);
?>
Micha
Thank you all for great help!!! :-)