仅从项目中选择一个项目,如果没有可用项目,则再次选择项目

For a special reason, I want to whenever I create a new profile, it picks a unique random avatar from my profile_avatars list. Then when all the avatars have already been linked to a profile one time it starts again. It means that an avatar can be linked to multiple profile.

I use two mySQL table which are profile_infos and profile_avatars

This is my php code so far which pick a random avatar :

$req_avatar  = $bdd->query('SELECT * FROM profile_avatars ORDER BY RAND() LIMIT 1');
$data_avatar = $req_avatar->fetch();
$rand = $data_avatar['avatar_address'];

this should work:

profile avatars table:

profile_avatars
------------------------------------
id | used_on_rotation | url | title |
-------------------------------------
1  |   0               | .. | ..    |
-------------------------------------
2  |   1               | . .| ..    |
-------------------------------------
...

when you registering, find avatar by

$bdd->query('SELECT * FROM profile_avatars where used_on_rotation = 0 ORDER BY RAND() LIMIT 1');
  1. if you get no results - that means all avatars have been used once already and the loop can be reset by

    update profile_avatars set used_on_rotation = 0

and then try to find avatar again

  1. if you get a $data_avatar , then run the query

    update profile_avatars set used_on_rotation = 0 where id = $DATA_AVATAR_ID