hanze/iwa-panda2

Model/Key.php in map-leaflet
Repositories | Summary | Log | Files | README.md

Key.php (5628B) download


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