doufang7385 2015-11-08 07:41
浏览 39
已采纳

如何为我的附加程序功能改进我的if else语句

now I would like to do a simple slave-gaming buy/sell function.

Now my database :T1, My data is:

+----------+------------+------------+-----------+------------+----------+
| uid      | exp        | lvl        | master    | slaveby    |bytime    |
+----------+------------+------------+-----------+------------+----------+
| 1        | 123        | 1          | 10        | 20         |12345     |
| 20       | 456        | 1          | 1         | 0          |12345     |
| 10       | 111        | 2          | 0         | 1          |12222     |
+------------------------------------------------------------------------+

uid is user ID. type int(11)

exp is user experience. type int(11)

lvl is user level. type int(11)

master means now the user is which uid's master. type int(11)

slaveby means now the user is which uid's slave. type int(11)

bytime is the time when user slaveby. type int(11)

In the table data, we can know row 1 was the user 1 was the user 10 master, but also was user 20's slave.

Now my coding run like this.

if ($_GET['mod'] == 'hireSlave'){
        mysql_query("UPDATE ".DB::table('game_goldwar_labour')." SET touid = '".$_GET['touid']."', bytime = '".$_GET[timestamp]."' WHERE uid = '".$_GET[uid]."'");
        mysql_query("UPDATE ".DB::table('game_goldwar_labour')." SET byuid = '".$_GET[uid]."', bytime = '".$_GET[timestamp]."' WHERE uid = '".$_GET['touid']."'");
};

But this coding the user only can be 1 user's master only.

I would like to change when user lvl 2, can be 2 slave's master, lvl3, can be 3 slave's master, maximun 5 slave, and also the bytime need to separate. Because I set when the bytime is reach the maximun time, the slave user will auto runaway. If this slave is in the second slot, will update the slave second slot as 0 (normally is user id of the slave, but because already runaway, so set to 0), after that, when the master hire slave when the slot is available.

slot available based on level and the slot was 0.

And also when user lvl1, cannot get 2 slave.

If I use if/else statement, will be a huge script.

So how to improve this? thank you.

  • 写回答

1条回答 默认 最新

  • drqvr26084 2015-11-08 08:20
    关注

    You need to use a separate table for storing slaves, for example have the columns: uid slave bytime (you also don't need to store both slaveby and master at the same time, that's redundant). When inserting a new record, check for the master's level to make sure the count is less than the number of slaves, otherwise don't insert a new record. I'm guessing you'd need an UNIQUE constraint in slave if a slave can only have one master at one time.

    For fetching a master's slaves simply do:

    SELECT slave FROM slaves WHERE uid = {uid}
    

    For fetching a slave's master simply do:

    SELECT uid FROM slaves WHERE slave = {slaveid}
    

    EDIT: Forgot to mention one thing, you really should do some input escaping and validation if you aren't already and use a DB layer like PDO to avoid SQL injections.

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

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料