-
Notifications
You must be signed in to change notification settings - Fork 0
/
frequencies.php
54 lines (47 loc) · 1.69 KB
/
frequencies.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
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Authorization, Content-Type");
header("Access-Control-Allow-Methods: GET, OPTIONS");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Max-Age: 3600");
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
handleOptionsRequest();
}
require_once 'db_connection.php';
require_once 'functions.php';
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
handleGetRequest($conn);
} else {
sendJsonResponse(405, ["message" => "$_SERVER[REQUEST_METHOD] requests are not allowed"]);
}
$conn->close();
function handleGetRequest($conn) {
global $frequencies_table_name;
if (isset($_GET['id'])) {
$id = $_GET['id'];
$stmt = $conn->prepare("SELECT * FROM $frequencies_table_name WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
if ($result !== false && $result->num_rows > 0) {
$frequency = $result->fetch_assoc();
sendJsonResponse(200, $frequency);
} else {
sendJsonResponse(404, ["message" => 'Frequency not found']);
}
$stmt->close();
} else {
$result = $conn->query("SELECT * FROM $frequencies_table_name");
$frequency = [];
while ($row = $result->fetch_assoc()) {
$frequency[] = $row;
}
sendJsonResponse(200, $frequency);
}
}
function handleOptionsRequest() {
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: OPTIONS, GET');
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');
exit;
}