1

first time poster here having a fair bit of trouble with something that may not be that complicated to fix. Firstly, I have extremely little experience with PHP, so I do apologize if I'm missing something obvious or if I ask for further clarification. Everything so far has been client side with HTML/JavaScript.

I'm looking to incorporate a registration system into my application. It's for a physiotherapist program where users details are stored within a JSON file. (I understand there are better alternatives).

The JSON contents currently is in this format:

{"users":[
    {
        "userid": "0",
        "password":"template",
        "physio": "[email protected]",
        "firstName":"template",
        "lastName":"template",
        "exerciseregime": {
            "exercisenumber":"5",
            "exercises":["2","1", "3", "5", "4"]
        }
    },
    {
        "userid": "1",      
        "password":"user",
        "physio": "[email protected]",
        "firstName":"User",
        "lastName":"Physio",
        "exerciseregime": {
            "exercisenumber":"1",
            "exercises":["2"]

        }
    },

And so forth. What I'm looking to do is use the details taken from the form and add them to the existing users array in this format.

The code I am currently using is

<?php
$file = "userjson.json";
$FirstName = $_POST["FirstName"];
$Surname = $_POST["Surname"];
$Password = $_POST["Password"];  
$Email = $_POST["email"];
$UserID = $_POST["UserID2"];

$json = json_decode(file_get_contents($file), true); 
$json["firstName"] = $FirstName;
$json["lastName"] = $lastName;
$json["password"] = $Password;
$json["physio"] = $Email;
$json["UserID"] = $UserID;



file_put_contents($file, json_encode($json));

?>

I used a previous answer found on the website to achieve this. The details are saved to the JSON file except not in the correct format. Rather than adding to the existing array as a new user, a new object is appended after the array rather than inside it. How would I go about fixing this and creating a new "user" inside the user array?

Also, a second question but not as important: How would I go about writing an array inside of this new object? I'm having a slight struggle in trying to add the "Exerciseregime" array to the object.

I would be extremely grateful for your help, and I am thankful for your consideration.

1 Answer 1

1

EDIT my first code was incorrect. I forgot to specify an ID and target the "users" array.

Try this :

$json = json_decode(file_get_contents($file), true); 
$nextid = sizeof($json['users']);
$json['users'][$nextid]['firstName'] = $FirstName;
$json['users'][$nextid]['lastName'] = $Surname;
$json['users'][$nextid]['password'] = $Password;
$json['users'][$nextid]['physio'] = $Email;
$json['users'][$nextid]['UserID'] = $UserID;

file_put_contents($file, json_encode($json));

Essentially each user has an ID key in that JSON array. You need to give the new user a key as well.

Also, you have $lastName on bottom and $Surname on top. May want to fix that as well. I added $Surname to the bottom to match the top.

To answer your second question

You would essentially just do:

$json['users'][$nextid]['exerciseregime']['exercisenumber'] = 1;
$json['users'][$nextid]['exerciseregime']['exercises'] = 2;
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic, I just added a reply about how the first code did not work, feels a bit out of place now with this fantastic demonstration so I removed it. I was able to go from the first example to being able to work things out myself, not as elegantly as you put it however! This is extremely helpful and will certainly shave a few hours of time off my deadline, many many thanks again!

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.