I need to update a json list of object via url post data. For example, with url:
http://myweb.com/index.php?name=Peter&surname=Brown
in php, using get method:
$name = $_GET["name"];
$surname = $_GET["surname"];
$json = array();
$json["nombre"] = $name;
$json["lat"] = $lat;
$data[] = $json;
$json_end= json_encode($data);
and json_end efectively is done like I want:
[{"name":"Peter","surname":"Brown"}]
My question is about how I can do it incrementing the json, in order to build an array like:
[{"name":"Peter","surname":"Brown"}]
[{"name":"newname","surname":"newsurname"}]
// and so on
each time user use the url with new parameters.
Do I need to write to a file, or to database? Any tips will be apreciated.
The idea is to be able that any user can add some dat using url. I tried to store the json to a fiel but the storing is durable only along the current session.
This would produce a single JSON. However I don’t know whether you meant to or not, but your output is a series of separate JSONs. You could do this by replacing the last line above with:
You would also probably want a way to reset/empty the array. In which case I’d change the if test from:
to:
Which you could use by visiting
Edit: If somewhere you add the following code:
You’ll get a list of the json elements enumerated by their respective keys. For example, it might look like:
0: "[{\"name\":\"Peter\",\"surname\":\"Brown\"}]" 1: "[{\"name\":\"newname\",\"surname\":\"newsurname\"}]" 2: "[{\"name\":\"George\",\"surname\":\"Washington\"}]" 3: "[{\"name\":\"John\",\"surname\":\"Adams\"}]"If you then add the following code:
you’ll be able to delete individual json entries by specifying id as the
delGET parameter.For example,
would delete the entry corresponding to
'[{"name":"George","surname":"Washington"}]';And the remaining entries would be:
0: "[{\"name\":\"Peter\",\"surname\":\"Brown\"}]" 1: "[{\"name\":\"newname\",\"surname\":\"newsurname\"}]" 3: "[{\"name\":\"John\",\"surname\":\"Adams\"}]" 4: "[{\"name\":\"Thomas\",\"surname\":\"Jefferson\"}]"