-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasubmit.php
60 lines (54 loc) · 1.81 KB
/
datasubmit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
$requestBody = file_get_contents('php://input');
$requestObject=json_decode($requestBody);
if($requestObject->operation=="insert"){
insertGameDetails($requestBody);
}
else if($requestObject->operation=="update"){
$inputGid=$requestObject->gid;
$inputName=$requestObject->name;
updateName($inputGid,$inputName);
}
//updateName($inputGid,$inputName);
//insertGameDetails("test");
function insertGameDetails($gameDetail){
//Load the file
$scoreDetails = file_get_contents('./data/scores.json');
//Decode the JSON data into a PHP array.
$scoreArray = json_decode($scoreDetails, true);
//insert gameDetail in array that has existing data
$gameDetailArray=json_decode($gameDetail, true);
$i=0;
foreach($gameDetailArray as $key=>$value) {
if($key=="operation"){
unset($gameDetailArray[$key]);
}
$i++;
}
$updatedGameDetail=json_encode($gameDetailArray);
echo $updatedGameDetail;
array_push($scoreArray, $updatedGameDetail);
//Encode the array back into a JSON string.
$json = json_encode($scoreArray);
//Save the file.
file_put_contents('./data/scores.json', $json);
}
function updateName($inputGid,$inputName){
//Load the file
$scoreDetails = file_get_contents('./data/scores.json');
//Decode the JSON data into a PHP array.
$scoreArray = json_decode($scoreDetails, true);
//Modify the Name property value on basis of gid key
foreach($scoreArray as $key => $value){
$valueObject=json_decode($value);
if($valueObject->gid==$inputGid){
$valueObject->name=$inputName;
$scoreArray[$key]=json_encode($valueObject);
}
}
//Encode the array back into a JSON string.
$json = json_encode($scoreArray);
//Save the file.
file_put_contents('./data/scores.json', $json);
}
?>