hanze/iwa-panda2

xml fix (fc195efe1bb213f3ca259b712bb0a89503130065)
Repositories | README.md

commit fc195efe1bb213f3ca259b712bb0a89503130065
parent cd4a5b2b559475a571a72e15a193aa9deef4d68a
Author: Friedel Schön <[email protected]>
Date:   Sun, 18 Jun 2023 23:49:14 +0200

xml fix

Diffstat:
MController/hdata/hdata.php18+++---------------
MModel/Key.php78++++++++++++++++++++++++++++++++++++++++++------------------------------------
2 files changed, 45 insertions(+), 51 deletions(-)

diff --git a/Controller/hdata/hdata.php b/Controller/hdata/hdata.php @@ -2,25 +2,13 @@ function make_table(){ $key = new Key(); if (isset($_GET['downloadXml'])) { - $combinedXml = new SimpleXMLElement('<data></data>'); + header('Content-Type: application/xml'); $humidityXml = $key->retrieveHumidityTable('ae9c50dc5cd58c538a0d6aedb17fffedcaffd568d22381dab3ae72baaeb24684', true); $hDataXml = $key->retrieveHData('ae9c50dc5cd58c538a0d6aedb17fffedcaffd568d22381dab3ae72baaeb24684', true); - // Load humidity XML into combined XML - $humidityDataNode = $combinedXml->addChild('humidityData'); - $humidityDataNode->addChild('data')->addChild('humidityData', $humidityXml->asXML()); - - // Load HData XML into combined XML - $hDataNode = $combinedXml->addChild('hData'); - $hDataNode->addChild('data')->addChild('hData', $hDataXml->asXML()); - - $xmlString = $combinedXml->asXML(); - - header('Content-Type: application/xml'); - header('Content-Disposition: attachment; filename="combined_data.xml"'); - - echo $xmlString; + echo "<weather><humidity>$humidityXml</humidity><hdata>$hDataXml</hdata></weather>"; + exit(); } else{ diff --git a/Model/Key.php b/Model/Key.php @@ -134,16 +134,19 @@ Class Key{ $html .= $table1 . $table2; if ($generateXml) { // Generate XML - $xml = new SimpleXMLElement('<data></data>'); + $xml = '<data>'; foreach ($results as $city => $entries) { foreach ($entries as $entry) { - $locationElement = $xml->addChild('location'); - $locationElement->addChild('city', $city); - $locationElement->addChild('date', $entry['date']); - $locationElement->addChild('humidity', $entry['humidity']); + $xml .= '<location>'; + $xml .= "<city>$city</city>"; + $xml .= "<date>{$entry['date']}</date>"; + $xml .= "<humidity>{$entry['humidity']}</humidity>"; + $xml .= '</location>'; } } + + $xml .= '</data>'; return $xml; // Return SimpleXMLElement object } @@ -194,46 +197,49 @@ Class Key{ $locations[$current_date][] = $city; } } - $html .= $this->retrieveHumidityTable('ae9c50dc5cd58c538a0d6aedb17fffedcaffd568d22381dab3ae72baaeb24684'); - - $html .= '<table id="htmlTable">'; - $html .= '<div class="space"></div>'; - $html .= '<div class="content-title"><h2>Windchill corrected temperature per day</h2></div>'; - - foreach ($results as $date => $entries) { - $html .= '<tr><th id="tabledate" colspan="4">' . date("d M Y", strtotime($date)) . '</th></tr>'; - $html .= '<tr><th>Location</th><th>Windchill</th></tr>'; - - foreach ($entries as $entry) { - $html .= '<tr>'; - $html .= '<td>'.$entry['city'].'</td>'; - $html .= '<td>'.$entry['windchill'].'</td>'; - $html .= '</tr>'; + + if (!$generateXml){ + $html .= $this->retrieveHumidityTable('ae9c50dc5cd58c538a0d6aedb17fffedcaffd568d22381dab3ae72baaeb24684'); + + $html .= '<table id="htmlTable">'; + $html .= '<div class="space"></div>'; + $html .= '<div class="content-title"><h2>Windchill corrected temperature per day</h2></div>'; + + foreach ($results as $date => $entries) { + $html .= '<tr><th id="tabledate" colspan="4">' . date("d M Y", strtotime($date)) . '</th></tr>'; + $html .= '<tr><th>Location</th><th>Windchill</th></tr>'; + + foreach ($entries as $entry) { + $html .= '<tr>'; + $html .= '<td>'.$entry['city'].'</td>'; + $html .= '<td>'.$entry['windchill'].'</td>'; + $html .= '</tr>'; + } } - } - - $html .= '</table>'; - - if ($generateXml) { - // Generate XML - $xml = new SimpleXMLElement('<data></data>'); + + $html .= '</table>'; + $html .= '<a href="?downloadXml=true" class="download-button">Download XML</a>'; + + return $html; + } else { + $xml = '<data>'; foreach ($results as $date => $entries) { - $dateElement = $xml->addChild('date', $date); - + $xml .= "<date at='$date'>"; foreach ($entries as $entry) { - $locationElement = $dateElement->addChild('location'); - $locationElement->addChild('city', $entry['city']); - $locationElement->addChild('windchill', $entry['windchill']); - $locationElement->addChild('humidity', $entry['humidity']); + $xml .= '<location>'; + $xml .= "<city>{$entry['city']}</city>"; + $xml .= "<windchill>{$entry['windchill']}</windchill>"; + $xml .= "<humidity>{$entry['humidity']}</humidity>"; + $xml .= '</location>'; } + $xml .= '</date>'; } + + $xml .= '</data>'; return $xml; // Return SimpleXMLElement object } - $html .= '<a href="?downloadXml=true" class="download-button">Download XML</a>'; - - return $html; } }