r109

Google Maps API PHP5 with DOMDocument

Mar 20th, 2013
46
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. require("db.php");
  4. // Start XML file, create  parent node
  5.  
  6. $doc = new DOMDocument('1.0');
  7. $node = $doc->createElement("markers");
  8. $parnode = $doc->appendChild($node);
  9.  
  10. // Opens a connection to a mySQL server
  11. $connection=mysql_connect('localhost', $username, $password);
  12. if (!$connection) {
  13.   die('Not connected : ' . mysql_error());
  14. }
  15.  
  16. // Set the active mySQL database
  17. $db_selected = mysql_select_db($database, $connection);
  18. if (!$db_selected) {
  19.   die ('Can\'t use db : ' . mysql_error());
  20. }
  21.  
  22. // Select all the rows in the markers table
  23. $query = "SELECT * FROM markers WHERE 1";
  24. $result = mysql_query($query);
  25. if (!$result) {
  26.   die('Invalid query: ' . mysql_error());
  27. }
  28.  
  29. header("Content-type: text/xml");
  30.  
  31. // Iterate through the rows, adding XML nodes for each
  32. while ($row = @mysql_fetch_assoc($result)){
  33.   // ADD TO XML DOCUMENT NODE
  34.   $node = $doc->createElement("marker");
  35.   $newnode = $parnode->appendChild($node);
  36.  
  37.   $newnode->setAttribute("name", $row['name']);
  38.   $newnode->setAttribute("address", $row['address']);
  39.   $newnode->setAttribute("lat", $row['lat']);
  40.   $newnode->setAttribute("lng", $row['lng']);
  41.   $newnode->setAttribute("type", $row['type']);
  42. }
  43.  
  44. echo $doc->saveXML();
  45. ?>
RAW Paste Data