Problem with a Random Image MySQL script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
w33nie
Forum Newbie
Posts: 12
Joined: Tue Jul 31, 2007 3:41 am
Location: Sydney

Problem with a Random Image MySQL script

Post by w33nie »

Hey guys,
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' />");
		}
	}
this is what the code prints on the page:

Code: Select all

<img src='i' height='120' width='600' />
Does anyone have any solutions for me? Because I dont know what to do from here.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Try something like:

Code: Select all

SELECT * from images where condition_here ORDER BY RAND() LIMIT 1
PS: OOps, you have images in a *ROW* ... sorry. The code above will be in use if you have two tables - one for data and one for images.

PPS: You can't have this line:

Code: Select all

$image_large = $image_large['SELECT ROUND(1 + (RAND() * $image_large_number))'];
Maybe something like this:

Code: Select all

$image_large = $image_large['image_large'. (rand(1, $image_large_number + 1))];
There are 10 types of people in this world, those who understand binary and those who don't
w33nie
Forum Newbie
Posts: 12
Joined: Tue Jul 31, 2007 3:41 am
Location: Sydney

Post by w33nie »

hmm no, that still gives me: <img src='i' height='120' width='600' />
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Ops :)

Code: Select all

$image_large = $row['image_large'. (rand(1, $image_large_number + 1))];
There are 10 types of people in this world, those who understand binary and those who don't
w33nie
Forum Newbie
Posts: 12
Joined: Tue Jul 31, 2007 3:41 am
Location: Sydney

Post by w33nie »

okay great, thanks that worked.

I had to remove the +1 though, because it would call up the number above image_large_number as well
Post Reply