Tealq Posted January 20 Share Posted January 20 Hello there, Im thinking about it and I dont know how to do it. That’s why Im asking here and I believe you can help me Im working on simple browser rpg, but I have a problem with codding inventory. My inventory works now like equip and unequip. I need add function, when I equip weapon, the weapon damage add to the player damage and when you want unequip weapon, the weapon damage delete from player damage. Can someone help me with this? Here is my code for equip: Many thanks! <?php $weaponid=$_GET['weaponid']; $reequip="UPDATE playeritem SET equip=0 WHERE pid='$id' AND equip=1"; mysqli_query($connection, $reequip) or die ("Problem with connection"); $equip="UPDATE playeritem set equip=1 where pid='$id' AND weaponid='$weaponid'"; mysqli_query($connection, $equip); echo "<big>Weapon equipped!<br></big>"; echo "<br><a href='inventory.php'>Go to inventory</a>" ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/317642-simple-player-inventory-i-need-help/ Share on other sites More sharing options...
Tealq Posted January 20 Author Share Posted January 20 (edited) Hello, I sorted this, now when I equip weapon, damage adding to the player and when weapon unequiped, weapon damage delete from player. But, I have still problem with inventory. When I equip weapon, still can equip more weapons from inventory. How to do: When weapon is equipped, cant equip another weapon until equiped weapon is enequipped? Here is code: Many thanks. while($inventory3=mysqli_fetch_array($inventory2)) { if ($inventory3['equip'] == 1) {$inventory3['equip'] = "Equipped <A href='equippedoff.php?weaponid=$inventory3[weaponid]&type=w'>Unequip";} else {$inventory3['equip'] = "<A href='equipped.php?weaponid=$inventory3[weaponid]&type=w'>equip";} Edited January 20 by Tealq Quote Link to comment https://forums.phpfreaks.com/topic/317642-simple-player-inventory-i-need-help/#findComment-1614252 Share on other sites More sharing options...
mac_gyver Posted January 20 Share Posted January 20 1 hour ago, Tealq said: weaponid presumably, you have a weapon table, that holds the definition of each weapon, with damage value for each weapon? the total damage is a derived value. you would query/calculate it when needed. you would use a JOIN query, between the playeritem table and the weapon table, to SUM() the damage value for each weapon that the user has. some points about the posted code - you should trim all input data, mainly so that you can detect if it was all white-space characters, then validate it before using it. if an input is required to be non-empty or must have a specific format, don't use it if it isn't valid. don't copy variables to other variables for nothing. you should use a post method form when performing an action on the servers, such as when inserting, updating, or deleting data. don't put external, dynamic values directly into sql query statements, where an sql special character can break the sql query syntax. use a prepared query instead. if it seems like using prepared queries with the mysqli extension is overly complicated, it is. this would be a good time to switch to the much simpler and more modern PDO extension. don't use or die() for error handling. you should instead use exceptions for database statement error handling (this is the default setting now in php8+) and only catch and handle database exceptions for user recoverable errors, such as when inserting/updating duplicate or out of range user submitted data. for all other query errors and other types of queries, simply do nothing in your code and let php catch and handle any database exception. don't create specific variable names for nothing. e.g. $reequip, $equip. the only variable names that must be unique are the result from any section of code that will be used elsewhere in the code. for these two variables, just use $sql or similar, since you will completely deal with one query before going on to do something else in the code. don't echo static markup. just drop out of php mode and put the markup inline. Quote Link to comment https://forums.phpfreaks.com/topic/317642-simple-player-inventory-i-need-help/#findComment-1614253 Share on other sites More sharing options...
Barand Posted January 20 Share Posted January 20 5 minutes ago, Tealq said: When weapon is equipped, cant equip another weapon until equiped weapon is enequipped? Are you saying only one weapon at a time can be removed from the armoury, or a player can have only one weapon at a time? Quote Link to comment https://forums.phpfreaks.com/topic/317642-simple-player-inventory-i-need-help/#findComment-1614254 Share on other sites More sharing options...
Tealq Posted January 20 Author Share Posted January 20 Hello, Yes, I have weapon table with name, type and damage value for each weapon and yes, I mean player can have only one weapon at a time. Okay, Im learning php just 1 - 2 months, so I learning from videos on youtube and topics on forums, so the code is not very well Thank you for tips. Quote Link to comment https://forums.phpfreaks.com/topic/317642-simple-player-inventory-i-need-help/#findComment-1614255 Share on other sites More sharing options...
Barand Posted January 20 Share Posted January 20 In that case, put a weapon id column in the player table. It's value will either be NULL (no weapon) or the weapon id of their weapon. You get the damage value by joining player table to the weapon table using weapon id. Quote Link to comment https://forums.phpfreaks.com/topic/317642-simple-player-inventory-i-need-help/#findComment-1614256 Share on other sites More sharing options...
Tealq Posted January 21 Author Share Posted January 21 Hello, many thanks ! I tried before something with adding damage to player with mysql query, but this is what I wanted Simple and working, just player damage + weapon damage Can you help me with one more thing, please? Im trying get image for each weapon, but Im not success. I make inventory in table, all working, showin Weapon name, damage... but IMG not working. I uploaded images with php to mysql, but I have problem get the image. here is code: <?php $weaponid = $weapon3['id']; $weapimg="SELECT * from weaponimg where weaponid='$weaponid'"; $weapimg2=mysqli_query($connection, $weapimg); $weapimg3=mysqli_fetch_array($weapimg2); ?> <tr> <td><img src="./uploads/<?php $weapimg3['filename']?>"</td> <td><?php print "$weapon3[name]" ?> </td> <td><?php print "$weapon3[damage]" ?></td> <td><?php print "$weapon3[level]" ?></td> <td><?php print "$weapon3[equip]" ?></td> </tr> <?php } ?> </tbody> </table> Quote Link to comment https://forums.phpfreaks.com/topic/317642-simple-player-inventory-i-need-help/#findComment-1614267 Share on other sites More sharing options...
mac_gyver Posted January 21 Share Posted January 21 did you look at the resulting markup in your browser's 'view source' of the page to see what might be wrong with it? Quote Link to comment https://forums.phpfreaks.com/topic/317642-simple-player-inventory-i-need-help/#findComment-1614270 Share on other sites More sharing options...
Barand Posted January 21 Share Posted January 21 35 minutes ago, Tealq said: but IMG not working. Have you checked that the image files are in the uploads folder? You have a separate table for weapon images??? Why not store the image file name in the weapon table? +--------------+ +--------------+ | player | | weapon | +--------------+ +--------------+ | player_id | +------| weapon_id | | name | | | name | | weapon_id |------+ | damage | +--------------+ | image_file | | level | +--------------+ SELECT w.image_file , w.name , w.damage , w.level , p.weapon_id IS NOT NULL as equip FROM weapon w LEFT JOIN player p USING (weapon_id) +------------+--------+--------+-------+-------+ | image_file | name | damage | level | equip | +------------+--------+--------+-------+-------+ | knife.jpg | Knife | 50 | 1 | 1 | | seax.jpg | Seax | 100 | 1 | 1 | | katana.jpg | Katana | 200 | 1 | 1 | | pistol.jpg | Pistol | 400 | 2 | 0 | | rifle.jpg | Rifle | 750 | 2 | 0 | | rpg.jpg | RPG | 1000 | 3 | 0 | +------------+--------+--------+-------+-------+ Quote Link to comment https://forums.phpfreaks.com/topic/317642-simple-player-inventory-i-need-help/#findComment-1614271 Share on other sites More sharing options...
Tealq Posted January 21 Author Share Posted January 21 In source code: <img src="./uploads/<br /> <b>Warning</b>: Trying to access array offset on value of type null in ..... the line where is img src. Anyway, you have a very nice profile picture! Yes, I checked the folder and image is in the folder. And yes, I have separate table for weapon images, but your way sound very good, Im going to try it. Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/317642-simple-player-inventory-i-need-help/#findComment-1614275 Share on other sites More sharing options...
mac_gyver Posted January 21 Share Posted January 21 (edited) unless you can have multiple images per weapon, now or in the future, you don't need a separate table for the images. if it is possible that a weapon doesn't have an image, you would either skip producing the markup for the image or display a general 'no image available' image instead. 51 minutes ago, Tealq said: <b>Warning</b>: Trying to access array offset on value of type null in this php error message indicates that the query failed due to an error (or perhaps your full code is overwriting or using the variable for something else), and that you don't have any error handling for database statements that can fail. see my first post in this thread, point #5 about using exceptions for database statement errors. a) you should be using php8+ and b) you should set the error mode to exceptions for the database extension you are using if you are not using php8+. edit: i see that, at least in the documentation, a null is returned when there is no (more) data to fetch (it used to be a false value for this condition.) if so, this indicates that the query didn't match any data. if this is a case of no image for a weapon, you need conditional logic to test for an image or not and take an appropriate action in the code. the snippet of code and variable naming also implies you are executing queries inside of loops. this is extremely inefficient and is resulting in a wall of unnecessary code/query(ies). when you have related data in multiple tables, you need to use an appropriate type of JOIN query to get all the data that you want in a single query, then simply loop over the result from that single query to produce the output that you want. Edited January 21 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/317642-simple-player-inventory-i-need-help/#findComment-1614276 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.