PDA

View Full Version : Making A Rank System In PHP



Mike
05-27-2005, 01:52 PM
Hi all.

I'm currently doing a project which involves giving "weight" to a row in a database. Basically, the more weight the row has, the more times it will be picked.

The problem, I don't know how to do what I've just said. First I was thinking about using rand(), but then realised it wouldn't really work with that.

I have run out of ideas, anyone know what I could do?

Cheers,
Mike

Westech
05-27-2005, 02:32 PM
Try this (untested) :



SELECT * FROM Table ORDER BY RAND()*(1/weight) LIMIT 1;


This assumes that each row has an integer field called 'weight'. The higher the number in this field the more likely it is for the row to be selected.

Mike
05-28-2005, 10:44 AM
Yup, that's it - I never knew you could use queries like that. Thanks Westech :)