Hello Angela,
On 16 Jan 2004 at 11:30, Angela K Hilton wrote:
> I'm trying to dynamically randomise a chunk of HTML that is used on a
> page, aka random image, so that each time a client opens the page [or
> refreshes] a part of the page changes.
>
> The chunks of HTML are saved in their own directory as .htm files. I
> was hoping to adapt the following code I have managed to cobble together
> to display random images:
>
> <?php
> // images in folder
> $total = "5";
If the value of $total is not going to change, why even use a variable?
> // file type
> $file_type = ".htm, .gif, .jpg";
What's this $file_type for?
> // dir location
> $image_folder = "dir/dir";
>
> $start = "1";
Again, if the value of $start is not going to change, why use a variable?
> $random = mt_rand($start, $total);
>
> $image_name = $random . $file_type;
This won't work. You'll end up with something like "$image_name = 2.htm, .gif, .jpg;",
which is clearly not what you want (or need).
> // print file name
> // echo "$image_name";
>
> // display image
> // echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";
>
> ?>
>
> I'd like the code to take the HTML and place it in a <div></div>,
> probably using a <?php require (""); ?> (I think I'm OK with this once
> the code is sorted).
Suggestion: have your PHP script open the image folder, read it's contents and load
all
file names it finds onto an array; then user array_rand() to randomly pick a key.
You'll
need to get the PHP manual (in case you still don't have it) from
http://www.php.net/download-docs.php and read up on opendir(), readdir() and
array_rand().
Good luck,
Erik