Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying x out of y random images from database

Status
Not open for further replies.

DrDan1

Technical User
Oct 2, 2002
127
GB
Hi. PHP newbie getting to grips with things.

I'v got a simple test page to display the fist 4 images from a database... but what I'd really like it to do is display 4 RANDOM images from the database without repeating any. i.e. each one should be unique.

I realise I could do this by having 4 variables and run if or while functions to prevent their from being any duplicates... but that get's messy if I need to have much more than 4, and I would like to have the possibility of changing the amount of 4 to 10, 20 or 100 in the future.

I suppose I would need some sort of array to do this but I'm not getting very far without help.

So, is there any way to use $RANDVAR in such an array?

Thanks,
Dan

----------------------------------------
Knowing is not enough, we must apply. Willing is not enough, we must do.
--So said the ever wise Bruce Lee
Memorize and live by it!
 
This is one of those things that is easier, faster, and better to do from the db itself than in PHP

You haven't told us what Db you are using, but assuming mysql, you could use a query like this prodce the results you want.

SELECT *from mytable ORDER BY rand() limit 20;

Will get you a random sampling of 20 records from your DB.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks, and yes it is MySQL. I heard though, that using the ORDER BY Rand() could be quite a bit slower than using php to generate random variables. I haven't got enoughData yet to actually test that, and I would like it to be as quick and streamlined as possible.

Which is ulimately better to use then?

----------------------------------------
Knowing is not enough, we must apply. Willing is not enough, we must do.
--So said the ever wise Bruce Lee
Memorize and live by it!
 
It is slower when you have lot of records. a lot being upwards of 100,000 records.

You could do it in PHP, by randomly selecting id numbers of the images, and the querying the DB for them.

Or by creating an array of numbers, and then using that to get the images from another array.

The Second method requires you tu query the database for all its records, and then select the ones you want, which is not recommended. as the you end up querying again thousands of records only to use 10 or 20.


While is distinctly possible, i think whatever speed advantage yo get from the PHP, you loose by havng to query the entire DB.

The better method would be to generate the amount of id numbers you need and then produce a query to retrieve those images.





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Do you mean something like using 'mysql_num_rows' to determine the total number of rows... and then randomly selecting 4 rows from 0 to Total number of rows? That would be quicker than running a query for ID numbers, wouldn't it?

That's pretty much the model I'm trying to work on now. I just don't know how to implement it without have 4 different variables for each of the 4 random rows. [ponder]




----------------------------------------
Knowing is not enough, we must apply. Willing is not enough, we must do.
--So said the ever wise Bruce Lee
Memorize and live by it!
 
By the way... HAPPY NEW YEAR!!

----------------------------------------
Knowing is not enough, we must apply. Willing is not enough, we must do.
--So said the ever wise Bruce Lee
Memorize and live by it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top