-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadjson.php
48 lines (41 loc) · 1.06 KB
/
readjson.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
<?php
// read json file into array of strings
$jsonstring = file_get_contents("userprofiles.json");
// save the json data as a PHP array
$phparray = json_decode($jsonstring, true);
// use GET to determine type of access
if (isset($_GET["access"])) {
$access = $_GET["access"];
} else {
$access = "all";
}
if (isset($_GET["request"])) {
$request = $_GET["request"];
} else {
$request = "";
}
$returnData = [];
// pull data if connection is student staff or alumni only
if ($access != "all") {
foreach ($phparray as $entry) {
if ($entry["connection"] == $access) {
$returnData[] = $entry;
}
}
}
// pull data if name or message contains the search text
else if ($request != "") {
foreach ($phparray as $entry) {
if (str_contains($entry["name"], $request) || str_contains($entry["message"], $request)) {
$returnData[] = $entry;
}
}
}
// return all
else {
$returnData = $phparray;
}
// encode the php array to json
$jsoncode = json_encode($returnData, JSON_PRETTY_PRINT);
echo ($jsoncode);
?>