1

To all, Thanks in advance for helping out.. Trying to get this google script working, done both tutorials and I cannot get the map pins from the database showing up on the map. XML is valid, connected to db, just the pins... Code is as follows

phpsqlajax_genxml2.php

<?php
header("Content-type: text/xml");
require("phpsqlajax_dbinfo.php");

function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr);   
$xmlStr=str_replace("'",'&#39;',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$xmlStr); 
return $xmlStr; 
} 

// Opens a connection to a MySQL server
$connection=mysql_connect ('localhost', $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}

// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 6";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}



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

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'type="' . $row['type'] . '" ';
echo '/>';
}

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

?>

Html:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps AJAX + mySQL/PHP Example</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
        type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[

var customIcons = {
  restaurant: {
    icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
    shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
  },
  bar: {
    icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
    shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
  }
};

function load() {
  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(47.6145, -122.3418),
    zoom: 13,
    mapTypeId: 'roadmap'
  });
  var infoWindow = new google.maps.InfoWindow;

  // Change this depending on the name of your PHP file
  downloadUrl("phpsqlajax_genxml2.php", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
      var name = markers[i].getAttribute("name");
      var address = markers[i].getAttribute("address");
      var type = markers[i].getAttribute("type");
      var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("lat")),
          parseFloat(markers[i].getAttribute("lng")));
      var html = "<b>" + name + "</b> <br/>" + address;
      var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon,
        shadow: icon.shadow
      });
      bindInfoWindow(marker, map, infoWindow, html);
    }
  });
}

function bindInfoWindow(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
}

function downloadUrl(url, callback) {
  var request = window.ActiveXObject ?
      new ActiveXObject('Microsoft.XMLHTTP') :
      new XMLHttpRequest;

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request, request.status);
    }
  };

  request.open('GET', url, true);
  request.send(null);
}

function doNothing() {}

//]]>
</script>
</head>

<body onload="load()">
<div id="map" style="width: 500px; height: 300px"></div>
</body>
</html>

phpsqlajax_dbinfo.php

<?
$username="$username";
$password="$password";
$database=" $database";
?>

Thanks for the help!

Here is the output url: http://opensourcefish.com/phpsqlajax_map_v3.html Let me know what else you need if anything!

Ok, changed the php to echo xml instead of using DOM and I get invalid xml... I think.. Now what? Or how do I enable DOM_xml b/c i'm totally snowed by that one... Thanks again ya'll

Edit: added the db php file as well and updated the others as suggested

MacD
  • 566
  • 3
  • 9
  • 27
  • which version of PHP are you using? To add to geocodezip's answer, domxml_new_doc() is no longer bundled with PHP 5. Check http://stackoverflow.com/questions/4753171/fatal-error-call-to-undefined-function-domxml-new-doc for more details – Rohit Oct 15 '13 at 16:07
  • Im on it! I'll check those threads and make some changes. Thanks for help – MacD Oct 15 '13 at 16:24
  • btw using version 5.2.17 – MacD Oct 15 '13 at 16:28
  • Now it seems as though my xml is showing up as valid, but still only a map with no pins... Getting TypeError: xml is null... Thoughts? – MacD Oct 15 '13 at 17:57
  • Awesome! Xml is now validating.. What am I missing tho, my map pins are still not showing up. Only the map.. I'm still getting the "xml is null" error and firefox points me to this line: var markers = xml.documentElement.getElementsByTagName("marker"); – MacD Oct 15 '13 at 19:36
  • Try adding the header line to the phpsqlajax_dbinfo.php instead of phpsqlajax_genxml2.php file – Rohit Oct 15 '13 at 19:39
  • Header line added as well and still the same result. My brain is exploding! – MacD Oct 15 '13 at 19:49
  • Remove it from phpsqlajax_genxml2.php and only put it in the dbinfo.php file, Also it would help if you could post the code for dbinfo.php with the username/password fields taken out. – Rohit Oct 15 '13 at 19:59
  • Do not echo the header, simply put the following line into the file ` header("Content-type: text/xml");` . Again, the code inside phpsqlajax_dbinfo.php would be helpful to look at. Dont forget to censor out the password and stuff though. – Rohit Oct 15 '13 at 20:08
  • Ok, should be all ready. Thanks again for your help! – MacD Oct 15 '13 at 20:22
  • Add the `header("Content-type: text/xml");` after `` As anything between ` ?>` will be executed as php. Because you put it outside, it is just being displayed on the browser and not executed as PHP. – Rohit Oct 15 '13 at 20:23
  • Ok, added the header inside ?>.. – MacD Oct 15 '13 at 20:30
  • hmm... add `ob_start();` before the header line ? and try adding both ob_start(); and header lines in the other php file as well... btw are you echoing or printing anything in the dbinfo php file ? By printing or echoing stuff, it will collide with the setting of header option. So comment out whatever you are outputting on line 2 of the dbinfo php file – Rohit Oct 15 '13 at 20:32
  • Same Result:( gonna edit code above to show exactly where I put it – MacD Oct 15 '13 at 20:36
  • what is on your line 2 of the dbinfo.php file? it seems to be causing the error, do not output/print/echo anything when you are setting the header. – Rohit Oct 15 '13 at 20:37
  • there is nothing there? just and that's it. I can delete it and rewrite it if that would help? – MacD Oct 15 '13 at 20:40
  • Something is being output for all the php files. Is there any other header files that would be doing that ? the php results in "(output started at /home1/mac4281/opensourcefish.com/phpsqlajax_dbinfo.php:2)" error meaning something is being output – Rohit Oct 15 '13 at 20:43
  • what you can also try doing is writing contents to an xml file and then retrieving it from the xml file – Rohit Oct 15 '13 at 20:46
  • Ok, made the changes.. no other files on the server now (deleted some) I'm going to and get it to work statically next and go from there. Thanks for all your help! – MacD Oct 15 '13 at 20:51
  • Could and .htaccess file be stopping the xml? – MacD Oct 15 '13 at 20:53
  • Check updated answer. Remove everything between `echo ''; ... and echo '';` and replace it with the content posted in the answer and also remove the header line. – Rohit Oct 15 '13 at 21:02
  • Did Solution 2 work for you ? – Rohit Oct 16 '13 at 01:39
  • Didnt work the first time but I'm pretty sure I screwed it up so I'm about ti give it another go and if that doesnt work I think I'm gonna start from scratch again.. – MacD Oct 16 '13 at 15:02
  • ok, i posted all of the code here: https://gist.github.com/anonymous/7010144 – Rohit Oct 16 '13 at 15:54
  • You are awesome Rohit!! Thank you! I got it to work. My problem was an additional header. Deleted that and we are rockin!!! – MacD Oct 16 '13 at 16:32

3 Answers3

1

Your xml feed isn't working.

opensourcefish.com/phpsqlajax_genxml.php

gives:


Fatal error: Call to undefined function domxml_new_doc() in /home1/mac4281/opensourcefish.com/phpsqlajax_genxml.php on line 5

See this question for details about that issue.

Community
  • 1
  • 1
geocodezip
  • 158,664
  • 13
  • 220
  • 245
0

The xml being generated is invalid. Take out the extra bracket from the marker tag:

 echo '<marker ';

This way the attributes are correctly placed within the marker tag. Edit:

Also, add the following header in your phpsqlajax_dbinfo.php file after the <? tag:

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

Solution 2:

Instead of echoing the xml data, put it in a xml file:

 $data= '<markers> ';

 while ($row = @mysql_fetch_assoc($result)){

 $data1 = '<marker ' . 'name="' . parseToXML($row['name']) . '" ' .
    'address="' . parseToXML($row['address']) . '" ' . 'lat="' . $row['lat'] . '" ' .
    'lng="' . $row['lng'] . '" ' . 'type="' . $row['type'] . '" ' . '/>';

 }

$data2 = '</markers>';

$xmlData = $data. $data1 . $data2;
file_put_contents("xmldata.xml", $xmlData);

and then load it in the html through:

   downloadUrl("xmldata.xml", function(data) {

Also add the following to the html as we need to load the php file first:

<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>

And the following line right before downloadUrl("xmldata.xml", function(data) { :

    $.get("phpsqlajax_genxml2.php");

So all the files now look like this: https://gist.github.com/anonymous/7010144

Rohit
  • 300
  • 1
  • 11
0

To all: Thank you so much! I got it working and it turned out to be a header issue and a really dumb mistake. Lesson to all you "copy/paste rangers" out there, READ YOUR CODE!!! Thanks a TON Rohit for all the help. Your were right early on, it was simply a duplicate header.. I copied but forgot to delete what I copied and it was down hill from there. Thanks again guys!!

MacD
  • 566
  • 3
  • 9
  • 27