I'm new to programming and I'm learning by coding a simple incremental game. Currently I am working out the inventor system. I have a cooking skill in the game with a script that will take raw items and either cook or burn them based on chance.
I'm now trying to work out an inventory system to hold the items after the action. I have a character JSON file (linked to a firebase UID)
{
"character":[
{
"Username" : "Tester01",
"cooklvl" : "1",
"cookxp" : "0"
}
],
"items" :[
{
"rf0000" : 0,
"rf0001" : 0,
"rf0002" : 0,
"rf0003" : 0,
"rf0004" : 0,
"rf0005" : 0,
"rf0006" : 0,
"rf0007" : 0
}
]
}
I want to add items to be stored here by ID using PHP to post them to my file server.
<?php
$cookitem = $_POST['cookitem'];
$a = "../json/";
$b = $_POST['uid'];
$c = ".json";
$filename = $a.$b.$c;
$json_object = file_get_contents($filename);
$data = json_decode($json_object, true);
$data['cookitem'] = ++$cookitem;
$json_object = json_encode($data, JSON_NUMERIC_CHECK);
file_put_contents($filename, $json_object);
?>
I have this currently but I don't want to have to add the items all at once and adjust quantity. I want to add and remove them dynamically with PHP.
So I would need to check something along the lines of: Does item"RF0051" exist in playerdata.json? If no add it and set quantity to 1. If it does exist add 1 to the quantity.
How could I go about doing this in php? As well, is there a better way I could be going about this? The end goal is to have a fairly large amount of items. Am I shooting myself in the foot by pushing to JSON in terms of growth and server resources?
If you know what string to compare with, you can save it in a variable$yourString
and then check with an if-statement if it exists:
<?php
$yourString = strtolower("RF0051");
$json_object = file_get_contents($filename);
$data = json_decode($json_object, true);
if($data['items'][$yourString] != null){
//It exists
}
?>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.