Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help with sorting results

1 view
Skip to first unread message

Nosferatum

unread,
Jan 4, 2010, 4:43:38 AM1/4/10
to
I am using the following script to display seven random images from a
folder. All images have a numerical value. I would like the output to
be sorted, but I am lost... anyone with any advice?
Here's the script:
<?php function RandomFile($folder='', $extensions='.*'){
// fix path:
$folder = trim($folder);
$folder = ($folder == '') ? './' : $folder;

// 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");
}

?>

"Álvaro G. Vicario"

unread,
Jan 4, 2010, 5:07:16 AM1/4/10
to
El 04/01/2010 10:43, Nosferatum escribi�:

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
--

Nosferatum

unread,
Jan 4, 2010, 6:36:16 AM1/4/10
to
On 4 Jan, 11:07, "Álvaro G. Vicario"
<alvaro.NOSPAMTH...@demogracia.com.invalid> wrote:
> --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
> --– Skjul sitert tekst –
>
> – Vis sitert tekst –

Shuffle?? Ooops, it seems that my extremely old and weak PHP-knowledge
have som deep, black holes... Now I feel more stuck than ever...

Captain Paralytic

unread,
Jan 4, 2010, 7:23:13 AM1/4/10
to

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?

Nosferatum

unread,
Jan 4, 2010, 7:30:56 AM1/4/10
to
> If you want it sorted, what criteria do you want to sort it on?– Skjul sitert tekst –
>
> – Vis sitert tekst –

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.

Jerry Stuckle

unread,
Jan 4, 2010, 7:42:50 AM1/4/10
to
>> If you want it sorted, what criteria do you want to sort it on?� Skjul sitert tekst �
>>
>> � Vis sitert tekst �

>
>
>
> 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
==================

"Álvaro G. Vicario"

unread,
Jan 4, 2010, 8:04:52 AM1/4/10
to
El 04/01/2010 12:36, Nosferatum escribi�:
> On 4 Jan, 11:07, "�lvaro G. Vicario"
> <alvaro.NOSPAMTH...@demogracia.com.invalid> wrote:
>> El 04/01/2010 10:43, Nosferatum escribi�:

>


> 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

Michael Fesser

unread,
Jan 4, 2010, 8:09:37 AM1/4/10
to
.oO(Nosferatum)

>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

Nosferatum

unread,
Jan 4, 2010, 1:40:59 PM1/4/10
to
> Micha– Skjul sitert tekst –
>
> – Vis sitert tekst –

Thank you all for great help!!! :-)

0 new messages