title: Creating a kml file from a mysql database with php
body: <p>I hope there is a php genius here somewhere who can help me with this.  I have a database table called <b>baeir</b>, it has the following fields:<br><b>
  <br>b_id  (primary key)
  <br>b_name 
  <br>hrepparid (does not need to be displayed)
  <br>stjarna (does not need to be displayed)
  <br>lat
  <br>lng
  <br>comments
  <br><br></b>
  The table contains a name of a farm (b_name), its geographical coordinates (lat, lng) and comments - if there are any.
  <br><br>
  What I need is to get this data into a kml file.  I have tried to go through the tutorial on google's webpage and I got it to work BUT once I try to edit, it goes to h***.  As far as I can understand, a kml file is basically an xml file but unfortunately my php skills are not advanced enough to handle this.
  <br><br>
  I hope someone can help :-))</p>
  
  <p>This is the code I have from google, and it looks a bit too...detailed for my liking.  Any ideas how to simplify the code so that I can get my variables into it and nothing else?</p>
  
  <pre><code>&lt;?php
  require('phpsqlajax_dbinfo.php');
  
  // Opens a connection to a MySQL server.
  
  $connection = mysql_connect ($server, $username, $password);
  
  if (!$connection) 
  {
    die('Not connected : ' . mysql_error());
  }
  // Sets the active MySQL database.
  $db_selected = mysql_select_db($database, $connection);
  
  if (!$db_selected) 
  {
    die('Can\'t use db : ' . mysql_error());
  }
  
  // Selects all the rows in the markers table.
  $query = 'SELECT * FROM markers WHERE 1';
  $result = mysql_query($query);
  
  if (!$result) 
  {
    die('Invalid query: ' . mysql_error());
  }
  
  // Creates the Document.
  $dom = new DOMDocument('1.0', 'UTF-8');
  
  
  // Creates the root KML element and appends it to the root document.
  $node = $dom-&gt;createElementNS('http://earth.google.com/kml/2.1', 'kml');
  $parNode = $dom-&gt;appendChild($node);
  
  // Creates a KML Document element and append it to the KML element.
  $dnode = $dom-&gt;createElement('Document');
  $docNode = $parNode-&gt;appendChild($dnode);
  
  
  // Creates the two Style elements, one for restaurant and one for bar, and append the elements to the Document element.
  $restStyleNode = $dom-&gt;createElement('Style');
  $restStyleNode-&gt;setAttribute('id', 'restaurantStyle');
  $restIconstyleNode = $dom-&gt;createElement('IconStyle');
  $restIconstyleNode-&gt;setAttribute('id', 'restaurantIcon');
  $restIconNode = $dom-&gt;createElement('Icon');
  $restHref = $dom-&gt;createElement('href', 'http://maps.google.com/mapfiles/kml/pal2/icon63.png');
  $restIconNode-&gt;appendChild($restHref);
  $restIconstyleNode-&gt;appendChild($restIconNode);
  $restStyleNode-&gt;appendChild($restIconstyleNode);
  $docNode-&gt;appendChild($restStyleNode);
  
  $barStyleNode = $dom-&gt;createElement('Style');
  $barStyleNode-&gt;setAttribute('id', 'barStyle');
  $barIconstyleNode = $dom-&gt;createElement('IconStyle');
  $barIconstyleNode-&gt;setAttribute('id', 'barIcon');
  $barIconNode = $dom-&gt;createElement('Icon');
  $barHref = $dom-&gt;createElement('href', 'http://maps.google.com/mapfiles/kml/pal2/icon27.png');
  $barIconNode-&gt;appendChild($barHref);
  $barIconstyleNode-&gt;appendChild($barIconNode);
  $barStyleNode-&gt;appendChild($barIconstyleNode);
  $docNode-&gt;appendChild($barStyleNode);
  
  // Iterates through the MySQL results, creating one Placemark for each row.
  while ($row = @mysql_fetch_assoc($result))
  {
    // Creates a Placemark and append it to the Document.
  
    $node = $dom-&gt;createElement('Placemark');
    $placeNode = $docNode-&gt;appendChild($node);
  
    // Creates an id attribute and assign it the value of id column.
    $placeNode-&gt;setAttribute('id', 'placemark' . $row['id']);
  
    // Create name, and description elements and assigns them the values of the name and address columns from the results.
    $nameNode = $dom-&gt;createElement('name',htmlentities($row['name']));
    $placeNode-&gt;appendChild($nameNode);
    $descNode = $dom-&gt;createElement('description', $row['address']);
    $placeNode-&gt;appendChild($descNode);
    $styleUrl = $dom-&gt;createElement('styleUrl', '#' . $row['type'] . 'Style');
    $placeNode-&gt;appendChild($styleUrl);
  
    // Creates a Point element.
    $pointNode = $dom-&gt;createElement('Point');
    $placeNode-&gt;appendChild($pointNode);
  
    // Creates a coordinates element and gives it the value of the lng and lat columns from the results.
    $coorStr = $row['lng'] . ','  . $row['lat'];
    $coorNode = $dom-&gt;createElement('coordinates', $coorStr);
    $pointNode-&gt;appendChild($coorNode);
  }
  
  
  $kmlOutput = $dom-&gt;saveXML();
  while (@ob_end_clean());
  header('content-type:text/xml;');
  echo $kmlOutput;
  
  ?&gt;
  </code></pre>