Page 1 of 1

rand image

Posted: Sat May 28, 2005 3:19 pm
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;
	}
}

Posted: Sat May 28, 2005 3:22 pm
by Skara

Code: Select all

SELECT image FROM tb WHERE country='somewhere' LIMIT 1
Why will that not work?

Posted: Sat May 28, 2005 3:27 pm
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?

Posted: Sat May 28, 2005 3:31 pm
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

Posted: Sat May 28, 2005 3:39 pm
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:

Posted: Sat May 28, 2005 3:54 pm
by ol4pr0
:roll:

Thanks.. I believe i need to study a bit more concerning Queries :)

Posted: Sat May 28, 2005 4:26 pm
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.