<?php
require("functions.php");

function parseToXML($htmlStr) 
{ 
  $xmlStr=str_replace('<','&#60;',$htmlStr); 
  $xmlStr=str_replace('>','&#62;',$xmlStr); 
  $xmlStr=str_replace('"','&#34;',$xmlStr);
  $xmlStr=str_replace("'",'&#39;',$xmlStr); 
  $xmlStr=str_replace("&",'&#38;',$xmlStr);
  $xmlStr=str_replace("Á",'&#193;',$xmlStr);
  $xmlStr=str_replace("á",'&#225;',$xmlStr);
  $xmlStr=str_replace("À",'&#192;',$xmlStr);
  $xmlStr=str_replace("á",'&#224;',$xmlStr);
  $xmlStr=str_replace("Ì",'&#204;',$xmlStr);
  $xmlStr=str_replace("ì",'&#236;',$xmlStr);
  $xmlStr=str_replace("Ù",'&#217;',$xmlStr);
  $xmlStr=str_replace("ù",'&#249;',$xmlStr);
  $xmlStr=str_replace("É",'&#201;',$xmlStr);
  $xmlStr=str_replace("é",'&#233;',$xmlStr);
  $xmlStr=str_replace("È",'&#200;',$xmlStr);
  $xmlStr=str_replace("è",'&#232;',$xmlStr);
  $xmlStr=str_replace("Ó",'&#211;',$xmlStr);
  $xmlStr=str_replace("ó",'&#243;',$xmlStr);
  $xmlStr=str_replace("Ò",'&#210;',$xmlStr);
  $xmlStr=str_replace("ò",'&#242;',$xmlStr);

  return $xmlStr; 
} 

// query for Fragerunde 1, Frage 4: anguria
$query = "SELECT DISTINCT(ort.plz) AS plz, 
    ort.ortsname AS ortsname, 
    ort.lat AS lat, 
    ort.lng AS lng, 
    antwort.antwort_informant AS response, 
    informant.geschlecht AS geschlecht, 
    informant.informant_alter AS inf_alter
    FROM antwort
    INNER JOIN informant
    ON antwort.informantid = informant.informantid
    INNER JOIN ort
    ON informant.ortid = ort.ortid
    WHERE antwort.fragerunde = 1
    AND antwort.fragenr = 4
    AND antwort.antwort_informant <> ''
    ORDER BY antwort.antwort_informant;";


$result = mysqli_query($db_server, $query);
if (!$result) {
  die('Invalid query: ' . mysqli_error());
}

header("Content-type: text/xml");

// Start XML file, echo parent node
echo '<markers>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysqli_fetch_assoc($result)) {
  $response = parseToXML($row['response']);
  // Add to the XML Document Node
  echo '<marker ';
  echo 'ortsname="' . parseToXML($row['ortsname']) . '" ';
  echo 'response="' . $response . '" ';
  echo 'lat="' . $row['lat'] . '" ';
  echo 'lng="' . $row['lng'] . '" ';
  if (preg_match("/^an.+, c/i", $response)) {
      echo 'type = "anguria_cocomero" ';
  } elseif (preg_match("/^[ai]n/i", $response)) {
    echo 'type="anguria" ';
  } elseif (preg_match("/^m/i", $response)) {
    echo 'type="melone" ';
  } elseif (preg_match("/^c[ou]/i", $response)) {
    echo 'type="cocomero" ';
  } elseif (preg_match("/^c[ei]/i", $response)) {
    echo 'type="citrone" ';
  } elseif (preg_match("/^z[iuo]/i", $response)) {
      echo 'type="zipangulu" ';
  } elseif (preg_match("/^pa/i", $response)) {
      echo 'type = "pateca" ';
  } else {
    echo 'type="altro" ';
  }
  echo 'sex=" ' . $row['geschlecht'] . '" ';
  echo 'age=" ' . $row['age'] . '" ';
  echo '/>';
}

// End of XML file
echo '</markers>';

?>
