Jump to content

Recommended Posts

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 by ralphix
Link to comment
https://forums.phpfreaks.com/topic/319181-weapon-upgrade-problem/
Share on other sites

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 |
+----------+--------+-------+------------------------------------+

 

  • Like 1

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.

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.