I'm having a crack at writing what would have been my first script that I thought of myself, but something's flawed in my logic/script, and the following just does not work.
What I'm trying to do, is create a script that will display a random image, if there is more than one image in the row, each time the user loads the page. I want to put this script on a few pages of a site. Each page may be different, and the number of images will change over time, but will never be over 5.
So what I've got in the table is 5 fields that will contain the image path, they are named, image_large1, image_large2, image_large3, image_large4 and image_large5. Each will have a different file path, but all may not be used, only one or two may be used. Which will at the end be printed in an image tag.
Then I also have one row saying how many images are on this row, called image_large_number. I would change it myself depending on the number of images in the row. So if there was 3 images, I would make the value of this field 3.
The idea behind the code is if image_large_number has a value of 1, it will skip any if statements and retain $image_large as image_large1. Therefore displaying the first image, and no other.
But if image_large_number has a value of more than 1, it will use the random number generator to rename $image_large to anything from image_large1 to image_large5.
and yes I know ROUND isnt the best thing to use, but I didn't like the way FLOOR rounds up all the time. What should I use there?
Here's the script:
Code: Select all
$query = 'SELECT * FROM `environment` LIMIT 0 , 1';
$result = mysql_query($query);
if($result) {
while($row = mysql_fetch_array($result)){
$image_large_number = $row['image_large_number'];
$image_large = $row['image_large1'];
if($image_large_number > 1) {
$image_large = $image_large['SELECT ROUND(1 + (RAND() * $image_large_number))'];
}
print ("<img src='$image_large' height='120' width='600' />");
}
}Code: Select all
<img src='i' height='120' width='600' />