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.

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

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂