-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetHouseOriginal.php
103 lines (78 loc) · 2.07 KB
/
getHouseOriginal.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
94
95
96
97
98
99
100
101
102
103
<?php
error_reporting(0);
$tablename = $_GET['name'];
//$tablename= 'helluhraun';
require_once "dbase_connection.php";
$connection = mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname);
// test for connection
/*if (mysqli_connect_errno()) {
die("Database connection failed: " . mysqli_connect_error() .
" ( " . mysqli_connect_errno() . ")");
}*/
if(!$connection){
echo ('ConnectionError');
exit();
}
$query = "SELECT * FROM $tablename ";
$result = mysqli_query($connection, $query);
if (!$result) {
die("ConnectionError");
}
$dates = [];
$status = [];
$available = [];
while ($row = mysqli_fetch_assoc($result)) {
$dates[] = ($row['date']);
$status[] = ($row['status']);
}
$datesUnique = array_unique($dates);
$uniqueLength = count($datesUnique);
$datesLength = count($dates);
$newdates = [];
$newstatus = [];
$disabled =[];
if ($tablename == 'dyngja') {
$maxOccupancy = 7;
} elseif ($tablename == 'helluhraun') {
$maxOccupancy = 11;
} elseif ($tablename == 'iridium') {
$maxOccupancy = 4;
} elseif ($tablename == 'gasdetect') {
$maxOccupancy = 6;
} else {
$maxOccupancy = 2;
}
foreach ($datesUnique as $key => $value) {
array_push($newdates, $value);
}
$nn=[];
$timestamp=[];
for ($i = 0; $i < $uniqueLength; $i++) {
$temp = $newdates[$i];
$newstatus[$i] = 0;
for ($j = 0; $j < $datesLength; $j++) {
if ($temp == $dates[$j]) {
$newstatus[$i] += $status[$j];
}
}
//$newdates[$i] = new DateTime($newdates[$i]);
array_push($nn, new DateTime($newdates[$i]));
$timestamp[$i] = date_timestamp_get($nn[$i])*1000;
$available[$i] = $maxOccupancy - $newstatus[$i];
}
//change all $timestamp and newdates from here to $timestamps
for ($i = 0; $i < $uniqueLength; $i++) {
if ($available[$i] == 0) {
array_push($disabled,$timestamp[$i]);
unset($timestamp[$i]);
//unset($newstatus[$i]);
unset($available[$i]);
}
}
$timestamp=array_values($timestamp);
//$newstatus=array_values($newstatus);
$available=array_values($available);
echo json_encode([$timestamp, $available,$disabled,$maxOccupancy]);
mysqli_free_result($result);
mysqli_close($connection);
?>