Re: Pulling a random image
| From: | Hugh Bothwell | Date: | Mon, 03 Sep 2001 18:04:36 +0000 |
| Subject: | Re: Pulling a random image | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-65780@lists.php.net to get a copy of this message | ||
"Brad R. C." <bradc@addictedusers.com> wrote in message
news:ELEKJPELJEFCJONHNNAOMEBCCJAA.bradc@addictedusers.com...
> But what my goal was is to make it where it goes through all the images
> before showing a random twice.
... if you actually want to do this, you will have to keep track
of which items you have seen in the last rotation.
> Now... I thought the following function did that, but for some reason it
is
> not...
Nope. You are just (inefficiently) choosing one item at random.
An equivalent-but-faster approach would be
SELECT * FROM user
WHERE valid=1 AND inrotation=1
ORDER BY RAND() LIMIT 1
If you really need random-order, once-per-cycle images,
you could achieve it by adding a 'views' field to the table;
initialize all entries to the same value (ie 0). Each time an image
is displayed, increment the views; when you choose an image,
choose from the pool where the views value is minimal, ie.
SELECT id,imgname FROM user
WHERE valid=1 AND inrotation=1 AND views=MIN(views)
ORDER BY RAND() LIMIT 1
UPDATE user SET views=views+1
WHERE id=$id
How's that?