php - swapping values in different tables if the data exist then value + 1 if data = 0 then delete -


i have table called bag:

+--------+----------+---------+----------+ | bag_id | chara_id | item_id | item_qty | +--------+----------+---------+----------+ |      1 |        1 |       2 |       26 | |      2 |        1 |       1 |       56 | |      3 |        3 |       1 |        4 | |      6 |        3 |       4 |        3 | |      7 |        4 |       4 |        3 | |      8 |        5 |       4 |        3 | |      9 |        6 |       4 |        2 | |     10 |        1 |       5 |        1 | |     14 |        1 |       8 |        1 | |     15 |        1 |       6 |        2 | |     18 |        1 |       4 |        4 | |     19 |        1 |       3 |        2 | |     29 |        8 |       1 |        1 | |     30 |        8 |       7 |        2 | |     33 |        6 |       2 |        1 | |     34 |        3 |       5 |        1 | |     35 |        3 |       8 |        3 | |     37 |        4 |       3 |        1 | |     45 |        3 |       3 |       14 | |     46 |        8 |       2 |        2 | |     60 |        8 |       5 |        2 | |     61 |        3 |       2 |       10 | |     74 |       12 |       2 |        1 | |     97 |       12 |       5 |        1 | |    103 |        3 |       6 |        1 | +--------+----------+---------+----------+ 

and have table called equipment:

+----------+----------+-----------+-------------+----------+---------+ | equip_id | chara_id | weapon_id | headgear_id | armor_id | ring_id | +----------+----------+-----------+-------------+----------+---------+ |        3 |        1 |         4 |           5 |        9 |       8 | |        5 |        3 |         2 |           5 |        3 |       8 | |        6 |        4 |         7 |           5 |        3 |       8 | |        7 |        5 |         4 |           5 |        3 |       8 | |        8 |        6 |         3 |           5 |        2 |       8 | |       10 |        8 |         3 |           5 |        2 |       8 | |       11 |        3 |         2 |           5 |        0 |       8 | |       12 |        3 |         2 |           5 |        0 |       8 | |       13 |       12 |         2 |           0 |        0 |       0 | |       14 |        5 |         4 |           0 |        0 |       0 | |       15 |        1 |         1 |           0 |        0 |       0 | |       16 |        1 |         0 |           0 |        6 |       0 | |       17 |        4 |         4 |           0 |        0 |       0 | |       18 |        8 |         2 |           0 |        0 |       0 | |       19 |        3 |         2 |           5 |        0 |       8 | +----------+----------+-----------+-------------+----------+---------+ 

scenario: player equips item bag equipment:

#unequipping: 1. if equipment id unequipped exist in bag bag item = item + 1    else    insert item bag  #equipping:    1.  bag item = bag item - 1 item_id = equipped item_id     else if bag item = 0 delete it. 

basically want swap item's equipment bag table, though twist quantity, wherein if item swapped existing in bag table + 1 quantity , if item bag table = 1 remove item table , equip item equipment table.

please pm me if couldn’t understand situation, driving me nuts!

this current code(if helps little):  $fieldnames   = array(1=>'weapon', 'armor', 'ring', 'headgear');          if (isset($fieldnames[$equip->item_type])) {             $field = $fieldnames[$equip->item_type].'_id';              $sql_unequip  = "insert bag(item_id, chara_id, item_qty)values(:item_id, :chara_id, 1) on duplicate key update item_qty = item_qty + 1";             $sql_equip    = "update equipment set $field   = :item_id chara_id = :chara_id";             $sql_unequip2 = "delete bag item_id = :item_id , chara_id   = :chara_id , item_qty = 0";           } 


Comments