-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatePosts.php
93 lines (69 loc) · 2.53 KB
/
updatePosts.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
$action = $_POST["action"];
if($action==1){
//ADDING a post
$postID = $_POST["postID"];
$user = $_POST["user"];
$postTitle = $_POST["postTitle"];
$postDesc = $_POST["postDesc"];
$postTime = $_POST["postTime"];
$json = file_get_contents("posts.txt");
if($json==""){
$data = [];
$newPost = array("postID"=> $_POST["postID"], "user"=> $_POST["user"], "postTitle"=> $_POST["postTitle"], "postDesc"=> $_POST["postDesc"], "postTime"=> $_POST["postTime"]);
array_push($data, $newPost);
file_put_contents('posts.txt', json_encode($data));
// header('Content-type: application/json');
echo json_encode( $data );
}else{
$json=str_replace('},]',"}]",$json);
$data = json_decode($json, true);
$newPost = array("postID"=> $_POST["postID"], "user"=> $_POST["user"], "postTitle"=> $_POST["postTitle"], "postDesc"=> $_POST["postDesc"], "postTime"=> $_POST["postTime"]);
array_push($data, $newPost);
file_put_contents('posts.txt', json_encode($data));
// header('Content-type: application/json');
echo json_encode( $data );
}
// echo "<pre>";
// print_r($data);
// echo "</pre>";
}else if($action==0){
$json = file_get_contents("posts.txt");
$json=str_replace('},]',"}]",$json);
$data = json_decode($json, true);
$postID = $_POST["postID"];
$data[$postID] = array("postID"=> $_POST["postID"], "user"=> $_POST["user"], "postTitle"=> $_POST["postTitle"], "postDesc"=> $_POST["postDesc"], "postTime"=> $_POST["postTime"]);
file_put_contents('posts.txt', json_encode($data));
// header('Content-type: application/json');
echo json_encode( $data );
// echo "<pre>";
// print_r($data);
// echo "</pre>";
}else if($action==2){
//Deleting a post
$json = file_get_contents("posts.txt");
$json=str_replace('},]',"}]",$json);
$data = json_decode($json, true);
unset($data[$_POST["postID"]]);
// foreach ($data as $post){
// if($post["postID"]==$_POST["postID"]){
//
// }
// }
if(count($data)==0){
file_put_contents('posts.txt', "");
// header('Content-type: application/json');
$data = [];
$newPost = array("postID"=> "", "user"=> "", "postTitle"=> "", "postDesc"=> "", "postTime"=> "");
array_push($data, $newPost);
echo json_encode( $data );
}else{
file_put_contents('posts.txt', json_encode($data));
// header('Content-type: application/json');
echo json_encode( $data );
}
// echo "<pre>";
// print_r($data);
// echo "</pre>";
}
?>