0

I'm working on a game that allows players to store items in a MySQL database. I've got a table that stores every player's items. It's slot based, with each item having a unique value. So the table would have something like: id, itemvalue, slot, playerID.

I'm trying to figure out a way in PHP to allow the player to withdraw or deposit an item in a specific slot.

I need help updating entries after an action (withdraw or deposit) has taken place. For example: Say a player can have 10 slots for themselves. If the player withdraws an item from slot 4, how do I loop through their items from slots 5 to 10 and update the values to be -1 for their slot.

Of course, I'm getting the information from the table, let's call it "ItemTable" by using:

$sql = mysql_query("SELECT * FROM ItemTable WHERE playerID ='$_playerID' ORDER BY id ASC LIMIT 10");
3
  • 4
    If you're just learning PHP, please, do not learn the obsolete mysql_query interface. It's awful and is being removed in future versions of PHP. A modern replacement like PDO is not hard to learn. A guide like PHP The Right Way can help explain best practices. Always be absolutely sure your user parameters are properly escaped or you will have severe SQL injection bugs. Commented Jan 19, 2015 at 18:02
  • Hum.. Are you sure that you really need the 'id' ? You could use as primary key in your table the pair (playerID,slotNumber) and so your problem would be VERY simpler ( something like 'UPDATE ITEMTABLE SET itemvalue=-1 WHERE playerID = ... and slotNum > 4) Commented Jan 19, 2015 at 18:12
  • I'll look into more modern ways of using PHP and MySQL. I've only started a few months, so I will find it no problem learning a new API. Commented Jan 20, 2015 at 18:46

2 Answers 2

1

Something like:

UPDATE ItemTable SET slot=slot-1 WHERE playerID='$_playerID' AND slot>$withdrawnItemSlot

That should shuffle all the other items up a slot.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much, that's the one that I went with and worked. Cheers.
0

Would be something like this:

"SELECT * FROM ItemTable WHERE id > (SELECT itemID FROM ItemTable WHERE playerID='$_playerID') t2 ORDER BY id ASC LIMIT 10"

I mean to update, you do that:

"UPDATE ItemTable WHERE id > (SELECT itemID FROM ItemTable WHERE playerID='$_playerID') t2 SET (here you go with your updates) ORDER BY id ASC LIMIT 10"

(not sure if select should be named in this case, if it won't work, just remove "t2" from above queries)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.