rand image

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
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

rand image

Post by ol4pr0 »

Oke.. Getting a random image from db no problem.

However i need something that gets a random image that meets the requirements of the select. So only images that are suppose to load up for a certen page, or country. Kinda confused on how to do that.

Basic :

Code: Select all

function rand_image( $aa,$bb,$cc ) {
	global $dbc;
	$res = $dbc->select("images" array('req1'=>"whtvr",'req2'=>"whtvr));
	srand((double)microtime()*1000000);
	$random=rand(1,count($res));
	$res2 = $dbc->select("images", array('id'=>$random) );
	foreach ($res2 as $img) {
			return $img->JC;
	}
}
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Code: Select all

SELECT image FROM tb WHERE country='somewhere' LIMIT 1
Why will that not work?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Skara wrote:

Code: Select all

SELECT image FROM tb WHERE country='somewhere' LIMIT 1
Why will that not work?
As mentioned, it's hard to judge based on the code posted as it doesn't describe fully (to me, but I might be stupid :wink:) what it actually base the selct on...

Code: Select all

SELECT * FROM table2 WHERE a=b AND c<>d ORDER BY RAND() LIMIT 1
...is another way to use random in a query. Worth adding?
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Cuase there are more than one image that meet the requirement.

So for example from a total of 200 images the query will give me 20 results. Of those 20 i want to pick a random one.

[edit]
i think ure second query indeed is a nice working option.


The query is done by ( maby to be extended )
City
Country
Category
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Told you that I didn't think clearly. Still betting that the "...order by rand() limit 1... approach should come in handy, narrowing it down with the a=b clause of choise.

It should give you one image, randomly ordered.

Edited:
Oh, and I hate it when you edit posts while I'm replying. :wink:
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

:roll:

Thanks.. I believe i need to study a bit more concerning Queries :)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

ol4pr0 wrote::roll:

Thanks.. I believe i need to study a bit more concerning Queries :)
Yah well, I'm still discovering new php functions from time to time, so... The manuals are well written, no doubt about that, but you seldom read them header to footer, so of course things get missed.
Post Reply