0810
09-28-2002, 05:02 AM
random picture
Hi how are you doing? I am trying to run this script but it is not work well. I mean that whenever I test this script, my pictures 2 of them are same one is different. I would like to do three pictures which are different whenever a user visits my site. Therefore they don't lose interest.
my php is 4.22
Here is script
<?php
//remember this array is indexed at 0
$images=array("Picture/Hideo.jpg","Picture/ito1.jpg","Picture/ito2.jpg","Picture/ito3.jpg",
"Picture/ito4.jpg","Picture/ito5.jpg","Picture/ito6.jpg","Picture/ito7.jpg","Picture/ito8.jpg");
//seed the random number generator once, and only once
mt_srand((double)microtime() * 1000000);
//count the number of values in the array
$imagesCnt = count($images);
//this can be set to the number of images you would like to display
$imageShow = 3;
/*this array will be used to store what values have already
been selected. It is setup like this so that it is declared
array but no values are entered*/
$randPicsChosen = array();
//loop based on the number of images you would like to show
for($cnt=0;$cnt<$imageShow;$cnt++){
/* select a random number, 0-8, remember your array
starts at 0!*/
$randPic=mt_rand(0,$imagesCnt-1);
/*if that picture has already been selected,keep trying until
we get a different picture*/
while(array_key_exists($randPic, $randPicsChosen)){
$randPic=mt_rand(0,$numbersCnt-1);
}//end while
//fill array with the chosen values so they are not duplicated
$randPicsChosen[$randPic] = $images[$randPic] ;
}//end for
?>
<table border=0>
<tr>
<?php
foreach($randPicsChosen as $picName){
echo("<td><img src=\"$picName\"></td>");
}
?>
</tr>
</table>
Hi how are you doing? I am trying to run this script but it is not work well. I mean that whenever I test this script, my pictures 2 of them are same one is different. I would like to do three pictures which are different whenever a user visits my site. Therefore they don't lose interest.
my php is 4.22
Here is script
<?php
//remember this array is indexed at 0
$images=array("Picture/Hideo.jpg","Picture/ito1.jpg","Picture/ito2.jpg","Picture/ito3.jpg",
"Picture/ito4.jpg","Picture/ito5.jpg","Picture/ito6.jpg","Picture/ito7.jpg","Picture/ito8.jpg");
//seed the random number generator once, and only once
mt_srand((double)microtime() * 1000000);
//count the number of values in the array
$imagesCnt = count($images);
//this can be set to the number of images you would like to display
$imageShow = 3;
/*this array will be used to store what values have already
been selected. It is setup like this so that it is declared
array but no values are entered*/
$randPicsChosen = array();
//loop based on the number of images you would like to show
for($cnt=0;$cnt<$imageShow;$cnt++){
/* select a random number, 0-8, remember your array
starts at 0!*/
$randPic=mt_rand(0,$imagesCnt-1);
/*if that picture has already been selected,keep trying until
we get a different picture*/
while(array_key_exists($randPic, $randPicsChosen)){
$randPic=mt_rand(0,$numbersCnt-1);
}//end while
//fill array with the chosen values so they are not duplicated
$randPicsChosen[$randPic] = $images[$randPic] ;
}//end for
?>
<table border=0>
<tr>
<?php
foreach($randPicsChosen as $picName){
echo("<td><img src=\"$picName\"></td>");
}
?>
</tr>
</table>