PDA

View Full Version : ramdom pictures with php problem


0810
09-08-2002, 11:05 PM
Problem with ramdom pictures
hi how are you doing? I am trying to put my pictures randamly. Whenever a user comes to my website, he can see different pictures. But my script didn't work at all. Just come out only same pictures whenever I test it.

my php is 4.2 Could you help my script.

Here is code

<?php
$pictures=array("Hideo.jpg","ito1.jpg","ito2.jpg","ito3.jpg",
"ito4.jpg","ito5.jpg","ito6.jpg","ito7.jpg","ito8.jpg");


shuffle($pictures);


?>
<html>
<head>
<title>Japanese Picture</title>
</head>
<body>
<center>
<h1>Japan</h1>
<table width=100%>
<tr>
<?
for ($i = 0; $i < 3; $i++)
{

echo "<td align= center><img src=\"";
echo $pictures[$i];
echo "\" width=100 height=100></td>";
}
?>
</tr>
</table>
</center>
</body>
</html>

Thank you very much

firepages
09-09-2002, 07:52 AM
hi, shuffle is not very good at shuffling :) - especially for smaller arrays... (also you are supposed to seed shuffle with srand() though that still does not appear to help on smaller arrays)

this was an alternative posted in the user notes of the manual


$c = count($pictures);
for ($i=0; $i<$c; $i++) {
$d = mt_rand(0, $i);
$tmp = $pictures[$i];
$pictures[$i] = $pictures[$d];
$pictures[$d] = $tmp;
}

try that instead of shuffle()ling the array