ralphix Posted March 20 Share Posted March 20 (edited) Hello there. Im working on my simple game, but I have a big problem now. Im trying create weapon upgrade system. Yes I know, the code is amateur. 😅 For next level weapon upgrade you need another item. Upgrade system working just in case, when the item is on the first line in the database. I tried while($itemneed=mysqli_fetch_array($itemcon2)) but still same problem.  Why the system working just when the item is on the first line in database and not working with items in other lines? For example: In DB I have:  Playerid,  id (itemid) 2        2 2        3 2        4 and working just with item 2. When upgrade need another item (3 or 4), comes echo you don't have item for upgrade!. Where is problem, please? I don't know what to do next. Many thanks!  $invcon="SELECT * from items where playerid='$id'"; $invcon2=mysqli_query($connection, $invcon); $itemupg=mysqli_fetch_array($invcon2); $itemcon="SELECT * from weapons where playerid='$id' and weapid='$weapid'"; $itemcon2=mysqli_query($connection, $itemcon) or die("could not select backpack"); $itemneed=mysqli_fetch_array($itemcon2); if($itemneed['level'] == 1) { $item = "2"; } elseif($itemneed['level'] == 2) { $item = "3"; } elseif($itemneed['level'] == 3) { $item = "4"; } if($itemupg['id'] != $item) { echo "you don't have item for upgrade!"; } else {   Edited March 20 by ralphix Quote Link to comment https://forums.phpfreaks.com/topic/319181-weapon-upgrade-problem/ Share on other sites More sharing options...
Barand Posted March 20 Share Posted March 20 Perhaps it has something to do with your only ever reading the first row of the query results Quote Link to comment https://forums.phpfreaks.com/topic/319181-weapon-upgrade-problem/#findComment-1618377 Share on other sites More sharing options...
Barand Posted March 20 Share Posted March 20 If I understood correctly (although I can't see what your data looks like) you may want something like this... TEST DATA TABLE: items TABLE: weapons +----------+--------+ +----+----------+--------+-------+ | playerid | itemid | | id | playerid | weapid | level | +----------+--------+ +----+----------+--------+-------+ | 2 | 2 | | 1 | 2 | 1 | 2 | | 2 | 3 | | 2 | 3 | 1 | 2 | | 2 | 4 | +----+----------+--------+-------+ | 3 | 2 | +----------+--------+ QUERY and RESULTS (Find which players have an item that is 1 greater than current weapon level) SELECT w.playerid , w.weapid , w.level , COALESCE(i.itemid, 'You don\'t have an item for upgrade') as item FROM weapons w LEFT JOIN items i ON w.playerid = i.playerid AND i.itemid = w.level + 1; +----------+--------+-------+------------------------------------+ | playerid | weapid | level | item | +----------+--------+-------+------------------------------------+ | 2 | 1 | 2 | 3 | | 3 | 1 | 2 | You don't have an item for upgrade | +----------+--------+-------+------------------------------------+ Â 1 Quote Link to comment https://forums.phpfreaks.com/topic/319181-weapon-upgrade-problem/#findComment-1618398 Share on other sites More sharing options...
ralphix Posted March 20 Author Share Posted March 20 Hello, yes, for each level of weapon player need another item. My code for upgrading working, I have just problem with checking inventory -> if player have the item what need for upgrade. The query reading just first row, where player id = '$id'. Weapon upgrade for level 2 working, because the first row is item 2, so the upgrade do sucessfuly. But upgrade for level 3 or 4 not working, because the items is in row 2 and row 3, so then comes echo: you don't have item for upgrade! If I delete item 2 from player invenotry database, the first row is item 3 and then upgrade for level 3 works. I tried while, mysqli_fetch_all, mysqli_fetch_assoc but problem is still same, query still reading just first row. Â Quote Link to comment https://forums.phpfreaks.com/topic/319181-weapon-upgrade-problem/#findComment-1618411 Share on other sites More sharing options...
ralphix Posted March 20 Author Share Posted March 20 I solved it! Many thanks anyway Quote Link to comment https://forums.phpfreaks.com/topic/319181-weapon-upgrade-problem/#findComment-1618419 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.