|
Posted by Daemon on 01/01/06 13:16
Michael wrote:
> Ok, first of all I think you are going about this the wrong way. First
> of all I think we should put the names of the pictures into an array,
> so mine will be car.jpg, cat.gif and cow.png.
>
> [code]
> <?php
> $picture_array = array("car.jpg","cat.gif","cow.png");
> ?>
> [/code]
>
> Ok so now the script knows the names of the files we may display. We
> need to make the script generate a random number now and put it into a
> variable. So.....
>
> [code]
> <?php
> $picture_array = array("car.jpg","cat.gif","cow.png");
> $rand_no = rand(0,3);
> ?>
> [/code]
>
> Finally the script must render its data into (X)HTML. So...
>
> [code]
> <?php
> $picture_array = array("car.jpg","cat.gif","cow.png");
> $rand_no = rand(0,3);
> echo "<img src=\"".$picture_array[$rand_no]."\" />";
> ?>
> [/code]
>
You also can use another method and that is not to limit your self the
defined array, instead you can create the array by using glob, for the
existing files, and simpy using count to return the value of items in
your existing array.
<?php
$picture_array = glob("*.*");
$rand_no = rand(0,"count($picture_array)");
echo "<img src=\"".$picture_array[$rand_no]."\" />";
?>
~ Daemon
Navigation:
[Reply to this message]
|