Random Images with php problem


#1

Hello,
I am using php to randomly display images using this code:

<?
$array = file('random.php');
shuffle($array);
for ($i=0; $i<1; $i++) {
echo $array[$i];
}
?>

My problem is their is randomly no image being displayed on the site, I have pleanty of images in the images folder that php is picking out from tandom.php.

Any help would be great
Thank You.


#2

I suspect that you have white space in your random.php file that adds an element to the array but no real data. Thus when that random element gets selected… no image shows.

Also if you are always looking for element 0 then why do a for loop? Just grab element 0 of the already randomized array.

[php]

<? $array = file('random.php'); shuffle($array); echo $array[0]; ?>

[/php]


#3

Thank you very much for your help.

You was right I had a few extra blank lines after the code had finished, removed them and works great now.

Also if you are always looking for element 0 then why do a for loop? Just grab element 0 of the already randomized array.

I followed a tutorial for this as am really new to php, have adopted your way as makes more sense.

Thanks again