douzi5214 2016-05-13 14:57
浏览 24
已采纳

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

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'];
  • 写回答

1条回答 默认 最新

  • doouzlrvb01417498 2016-05-13 15:08
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器