Key.php (4790B) download
1<?php
2include 'Controller/api/api.php';
3class Key
4{
5 public function retrieveHumidityTable($key, $generateXml = false){
6 get_graph();
7 $html = '<table>';
8 $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "panda");
9 $query = "SELECT humidity, city, date_time
10 FROM retrieve_facilities
11 WHERE date(date_time) BETWEEN DATE_SUB(CURDATE(), INTERVAL 28 DAY) AND CURDATE()
12 ORDER BY date_time DESC";
13 $stmt = mysqli_prepare($db, $query);
14 $stmt->execute();
15 $data = $stmt->get_result();
16
17 $results = array();
18
19 $table1 = '<tr><th id="tabledate" colspan="4">Chengdu Humidity Data</th></tr>';
20 $table1 .= '<tr><th>Date</th><th>Humidity</th></tr>';
21 $table2 = '<tr><th id="tabledate" colspan="4">Kangding Humidity Data</th></tr>';
22 $table2 .= '<tr><th>Date</th><th>Humidity</th></tr>';
23 $xml1 = '<location name="Chengdu">';
24 $xml2 = '<location name="Kangding">';
25 while ($row = $data->fetch_assoc()) {
26 $humidity = $row['humidity'];
27 $current_date = $row['date_time'];
28 $correct_date = date("d M Y H:i:s", strtotime($current_date));
29 $city = $row['city'];
30
31 $results[$city] = array();
32
33 $results[$city][] = array(
34 'date' => $correct_date,
35 'humidity' => $humidity
36 );
37
38 // Table and Generate XML
39 if ($city == 'Chengdu') {
40 $table1 .= '<tr><td>' . $correct_date . '</td><td>' . $humidity . '</td></tr>';
41 $xml1 .= '<data>' . '<date>' . $correct_date . '</date><humidity>' . $humidity . '</humidity>' . '</data>';
42 } else {
43 $table2 .= '<tr><td>' . $correct_date . '</td><td>' . $humidity . '</td></tr>';
44 $xml2 .= '<data>' . '<date>' . $correct_date . '</date><humidity>' . $humidity . '</humidity>' . '</data>';
45 }
46 }
47 $xml1 .= '</location>';
48 $xml2 .= '</location>';
49 $xml = $xml1 . $xml2;
50 $html .= $table1 . $table2;
51 if ($generateXml) {
52 return $xml; // Return SimpleXMLElement object
53 }
54 return $html;
55 }
56
57
58 public function retrieveHData($key, $generateXml = false){
59 get_windchill();
60 $html = '';
61 $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "panda");
62 $query = "SELECT distinct wind_chill, date, city
63 FROM retrieve_zoos
64 WHERE date BETWEEN DATE_SUB(CURDATE(), INTERVAL 28 DAY) AND CURDATE()
65 ORDER BY date DESC, wind_chill
66 ";
67
68 $stmt = mysqli_prepare($db, $query);
69 $stmt->execute();
70 $data = $stmt->get_result();
71 $xml = '<WCTPD name="Windchill corrected temperature per day">';
72
73 $results = array();
74 $locations = array();
75
76 while ($row = $data->fetch_assoc()) {
77 $wind_chill = $row['wind_chill'];
78 $current_date = $row['date'];
79 $city = $row['city'];
80
81 if (!isset($results[$current_date])) {
82 $results[$current_date] = array();
83 $locations[$current_date] = array();
84 }
85
86 if (!in_array($city, $locations[$current_date]) && count($results[$current_date]) < 5) {
87 $results[$current_date][] = array(
88 'city' => $city,
89 'windchill' => $wind_chill,
90 );
91
92 $locations[$current_date][] = $city;
93 }
94 }
95 $html .= $this->retrieveHumidityTable('ae9c50dc5cd58c538a0d6aedb17fffedcaffd568d22381dab3ae72baaeb24684');
96
97 $html .= '<table id="htmlTable">';
98 $html .= '<div class="space"></div>';
99 $html .= '<div class="content-title"><h2>Windchill corrected temperature per day</h2></div>';
100
101 foreach ($results as $date => $entries) {
102 $html .= '<tr><th id="tabledate" colspan="4">' . date("d M Y", strtotime($date)) . '</th></tr>';
103 $html .= '<tr><th>Location</th><th>Windchill</th></tr>';
104
105 foreach ($entries as $entry) {
106 $citycity = $entry['city'];
107 $xml .= '<data>' . '<city>' . $citycity . '</city>' . '<wind_chill>' . $entry['windchill'] . '</wind_chill>' . '</data>';
108 $html .= '<tr>';
109 $html .= '<td>'.$entry['city'].'</td>';
110 $html .= '<td>'.$entry['windchill'].'</td>';
111 $html .= '</tr>';
112 }
113 }
114
115 $html .= '</table>';
116 $html .= '<a href="?downloadXml=true" class="download-button">Download XML</a>';
117 $xml .= '</WCTPD>';
118 if ($generateXml) {
119 return $xml; // Return SimpleXMLElement object
120 }
121 return $html;
122 }
123}