Jump to content

Help with PHP/MYSQL


dkar

Recommended Posts

Hi,

I just started learning php and I face some problems with my code. I am trying to read coordinates from a table in a database and display them as markers on a google map. I did this but then I want to have a drop down menu and by selecting a specific value (eg activity = climbing) - the corresponding marker should appear.

I have two separate files of code. In the php code, in the sql query (in red) if I have in the WHERE clause: name='$category' the code returns nothing. If I put a specific value from the table (like name='restaurant') then it works fine. So, it seems that it can not get the value from the dropdown menu which is in the second file. It must be something very simple that I am missing here. Some logical mistake I guess. Any ideas?

Why I can not get the value of the dropdown menu?

 

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

$category = $_POST['category'];

function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','<',$htmlStr); 
$xmlStr=str_replace('>','>',$xmlStr); 
$xmlStr=str_replace('"','"',$xmlStr); 
$xmlStr=str_replace("'",''',$xmlStr); 
$xmlStr=str_replace("&",'&',$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
[color=red]$query = "SELECT * FROM markers WHERE name='$category'";[/color]
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_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 = 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>';

?>

 

 

***************

 

!DOCTYPE html >
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>PHP/MySQL & Google Maps Example</title>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></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("markers.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>
    <div id="map" style="width: 500px; height: 300px"></div>

<form>
[color=red] <label> choose activity:
		<select name="category">
        <option value="Pan Africa Market">Pan Africa Market</option>
        <option value="Golf">Golf</option>
        <option value="Hiking">Hiking</option>
    	</select>
</label>[/color]
</form>
    <input type="submit" onClick="load()">

  </body>

</html>

 

Link to comment
Share on other sites

One way is to merge both the PHP and JavaScript code. For example, here's a simplified code snippet from a Google Map I created earlier in the year.

 

<?php
$sql = "SELECT id, name, latLong FROM tableName ORDER BY name";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
?>
//ADD MAP MARKER FOR CURRENT ENTRY
var marker<?php print $row['id']; ?> = new google.maps.Marker({
	position: new google.maps.LatLng(<?php print $row['latLong']; ?>),
	map: map,
	title:"<?php print $row['name']; ?>"
});
<?php
}
?>

 

 

If you prefer to add the markers with XML, the above code could be modified to meet those needs.

 

 

Instead of loading the map after the submit button is clicked, you would load it when the PHP part is done using something like this:

https://developers.google.com/maps/documentation/javascript/tutorial#asynch

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.