sorting random values from a string

sorting random values from a string

am 06.09.2007 15:39:06 von pt36

Hi
I have a php string like this:
$string = "one two three four five"
I have to sorting the values randomly every time the page are loaded.
So, for example:
first time
"one two three four five"
second time
"three five one four two"
third time
"five four two one three"
....
The number are a images in a folder and I have to sort these images in
a random sequence.
Thanks for suggestions.

Re: sorting random values from a string

am 06.09.2007 15:47:51 von luiheidsgoeroe

On Thu, 06 Sep 2007 15:39:06 +0200, pt36 wrote:

> Hi
> I have a php string like this:
> $string =3D "one two three four five"
> I have to sorting the values randomly every time the page are loaded.
> So, for example:
> first time
> "one two three four five"
> second time
> "three five one four two"
> third time
> "five four two one three"
> ...
> The number are a images in a folder and I have to sort these images in=

> a random sequence.
> Thanks for suggestions.


$string =3D "one two three four five";
$array =3D explode(' ', $string);
shuffle($array);
$string =3D implode(' ',$string);
-- =

Rik Wasmus

Re: sorting random values from a string

am 06.09.2007 16:50:20 von luiheidsgoeroe

On Thu, 06 Sep 2007 15:47:51 +0200, Rik Wasmus =

wrote:

> On Thu, 06 Sep 2007 15:39:06 +0200, pt36 wrote=
:
>
>> Hi
>> I have a php string like this:
>> $string =3D "one two three four five"
>> I have to sorting the values randomly every time the page are loaded.=

>> So, for example:
>> first time
>> "one two three four five"
>> second time
>> "three five one four two"
>> third time
>> "five four two one three"
>> ...
>> The number are a images in a folder and I have to sort these images i=
n
>> a random sequence.
>> Thanks for suggestions.
>
>
> $string =3D "one two three four five";
> $array =3D explode(' ', $string);
> shuffle($array);
> $string =3D implode(' ',$string);

....ofcourse:
$string =3D implode(' ',$array);




-- =

Rik Wasmus

Re: sorting random values from a string

am 06.09.2007 17:26:25 von pt36

Ok Rik
thanks for your help, but please tell me why this work
$string = "uno.jpg due.jpg tre.gif quattro.gif cinque.jpg";
$array = explode(' ', $string);
shuffle($array);
$string = implode(' ', $array);
echo $string ;
?>

and this not work
if ($handle = opendir('banner/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$file = $file . " ";
$array = explode(' ', $file);
shuffle($array);
$file = implode(' ', $array);
echo $file;
} }
closedir($handle);
}
?>

Re: sorting random values from a string

am 06.09.2007 17:38:41 von luiheidsgoeroe

On Thu, 06 Sep 2007 17:26:25 +0200, pt36 wrote:

> Ok Rik
> thanks for your help, but please tell me why this work
> > $string =3D "uno.jpg due.jpg tre.gif quattro.gif cinque.jpg";
> $array =3D explode(' ', $string);
> shuffle($array);
> $string =3D implode(' ', $array);
> echo $string ;
> ?>
>
> and this not work
> > if ($handle =3D opendir('banner/')) {
> while (false !== ($file =3D readdir($handle))) {
> if ($file !=3D "." && $file !=3D "..") {
> $file =3D $file . " ";

Because a file might have a space in it, and it might be a directory? It=
's =

quite nonsense to keep juggling between an array & a string.
=

if ($handle =3D opendir('banner/')) {
$files =3D array();
while (false !== ($file =3D readdir($handle))) {
if (is_file($file)) $files[] =3D $file;
}
shuffle($files);
echo implode(' ',$files);
closedir($handle);
}
?>
-- =

Rik Wasmus

Re: sorting random values from a string

am 06.09.2007 18:12:40 von pt36

Sorry Rik but this code not display nothing in my page
(in the directory banner I have some file)

if ($handle = opendir('banner/')) {
$files = array();
while (false !== ($file = readdir($handle))) {
if (is_file($file)) $files[] = $file;
}
shuffle($files);
echo implode(' ',$files);
closedir($handle);}

?>
I made mistake ?
Thanks

Re: sorting random values from a string

am 06.09.2007 18:15:55 von luiheidsgoeroe

On Thu, 06 Sep 2007 18:12:40 +0200, pt36 wrote:

> Sorry Rik but this code not display nothing in my page
> (in the directory banner I have some file)
>
> > if ($handle =3D opendir('banner/')) {
> $files =3D array();
> while (false !== ($file =3D readdir($handle))) {
> if (is_file($file)) $files[] =3D $file;
> }
> shuffle($files);
> echo implode(' ',$files);
> closedir($handle);}
>
> ?>
> I made mistake ?


Nope, I did :P

$dir =3D './banner';
if ($handle =3D opendir($dir)) {
$files =3D array();
while (false !== ($file =3D readdir($handle))) {
if (is_file($dir.'/'.$file)) $files[] =3D $file;
}
shuffle($files);
echo implode(' ',$files);
closedir($handle);}

?>


-- =

Rik Wasmus

Re: sorting random values from a string

am 06.09.2007 18:24:24 von pt36

Thanks Rik now work well
Have good day