Jump to content

[SOLVED] problem with variables


jakebur01

Recommended Posts

I got my google map working here. I was trying to add an info window to it. When I enter my variable $Address in all I get back is null. I had to do that urlencode on my variable earlier in the script. How can I pass my variable down correctly so I will be able to display my info in the info window?

 

  <?php

   // Your Google Maps API key
   $key = "mykey";

   // Connect to the MySQL database
   $conn = mysql_connect("localhost", "myuser", "mypass");

   // Select the database
   $db = mysql_select_db("mydb");

   // Query the table
   $query = "SELECT id, Address, City, State FROM smithssc WHERE id=$id";
   $result = mysql_query($query) or die(mysql_error());

   // Loop through each row, submit HTTP request, output coordinates
   while (list($id, $Address, $City, $State) = mysql_fetch_row($result))
   {
      
  $mapaddress = urlencode("$Address $City $State");

      // Desired address
      $url = "http://maps.google.com/maps/geo?q=$mapaddress&output=xml&key=$key";

      // Retrieve the URL contents
      $page = file_get_contents($url);

      // Parse the returned XML file
      $xml = new SimpleXMLElement($page);

      // Parse the coordinate string
      list($longitude, $latitude, $altitude) = explode(",", $xml->Response->Placemark->Point->coordinates);

      // Output the coordinates
      //echo "latitude: $latitude, longitude: $longitude <br />";

   }

?>
<script src="http://maps.google.com/maps?file=api&v=2&key=mykey"
      type="text/javascript"></script>
    <script type="text/javascript">
    //<![CDATA[
    function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
map.addControl(new GMapTypeControl());
map.addControl(new GLargeMapControl());

map.setCenter(new GLatLng(<?php echo"$latitude"; ?>,<?php echo "$longitude"; ?>), 14);
    map.openInfoWindow(map.getCenter(),
document.createTextNode(<?php echo"$Address"; ?>));

        
  }
    }
    //]]>
    </script>

Link to comment
https://forums.phpfreaks.com/topic/48569-solved-problem-with-variables/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.