开发者

Random result display PHP

开发者 https://www.devze.com 2023-04-13 08:55 出处:网络
I am working on a site that matches up users, just like any dating website. Except, I would like the users matches to randomly be displayed 1 at a time. Like they just click next until the come across

I am working on a site that matches up users, just like any dating website. Except, I would like the users matches to randomly be displayed 1 at a time. Like they just click next until the come across a result they like. I know how to query the results in a list format but what sort of code would I need to use to randomly display their match's profiles, randomly chosen (based on the match score), one at a time? I am using PHP. Thanks! If you even tell me what to look up that would b开发者_如何学Goe great!


You SHOULD NOT use mysql's ORDER BY RAND() clause because it's slow, it's better to act like this:

  1. select max(id) from profiles
  2. select profile.* from profiles where profile.id = mt_rand(0, MAX_ID)

will be much more better for performance


I suppose you have a "complex" mysql request.. You can just add an ORDER BY RAND(), and process that list using ajax for instance.


rand

or mt_rand


SELECT ...
FROM yourtable
WHERE .... search conditions ...
ORDER BY rand()
LIMIT 1

Is the easiest, but most likely least efficient method of doing this. Re run the query each time they click next and you'll get a random entry of the matches. Note that this won't prevent duplicates from showing up, which is very likely if there's very few matches.

0

精彩评论

暂无评论...
验证码 换一张
取 消