I wonder whether someone may be able to help me please.
I am using the following pieces of coding to successfully show map markers at particular locations held within a mySQL database.
PHP CODE
<?php
require("phpfile.php");
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Opens a connection to a MySQL server
$connection=mysql_connect ("hostname", $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 locationid, detectorid, searchheadid FROM finds WHERE `locationid` = '43'";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("locationid",$row['locationid']);
$newnode->setAttribute("detectorid",$row['detectorid']);
$newnode->setAttribute("searchheadid",$row['searchheadid']);
}
echo $dom->saveXML();
?>
HTML CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Location</title>
<link rel="stylesheet" href="css/findsperlocationstyle.css" type="text/css" media="all" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=en"></script>
<script type="text/javascript">
var customIcons = {
Artefact: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
Coin: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_green.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
Jewellery: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_yellow.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
}
};
// Creating a LatLngBounds object
var bounds = new google.maps.LatLngBounds();
function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(54.312195845815246,-4.45948481875007),
zoom:14,
mapTypeId: 'satellite'
});
// Change this depending on the name of your PHP file
downloadUrl("phpfile.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markers.length; i++) {
var locationid = markers[i].getAttribute("locationid");
var detectorid = markers[i].getAttribute("detectorid");
var searchheadid= markers[i].getAttribute("searchheadid");
var icon = customIcons[findcategory] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
title: 'Click to view details',
icon: icon.icon,
shadow: icon.shadow,
formdetectorid: detectorid,
formsearchheadid: searchheadid,
});
bounds.extend(point);
map.fitBounds(bounds);
google.maps.event.addListener(marker, "click", function() {
document.getElementById('detectorid').value = this.formdetectorid;
document.getElementById('searchheadid').value = this.formsearchheadid;
});
}
});
}
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()">
<form name="findsperlocation" id="findsperlocation">
<p align="left"><label>Location id<br />
</label>
</p>
<div>
<div align="left">
<input name="locationid" type="text" id="locationid" value="43" readonly="readonly"/>
</div>
</div>
<p align="left"><label>Detector Used</label> </p>
<div>
<div align="left">
<input name="detectorid" type="text" id="detectorid" size="30" maxlength="30"/>
</div>
</div>
<p align="left"><label>Search Head Used</label> </p>
<div>
<div align="left">
<input name="searchheadid" type="text" id="searchheadid" size="30" maxlength="30"/>
</div>
</div>
</form>
<div id="map"></div>
</body>
</html>
The problem I have concerns two of my fields, 'detectorid' and 'searchheadid'. The information for these fields is saved via two drop down menus on another form. The drop down menus show the appropiate text values for the user to choose, but the 'id' value associated with each selection is saved to the table which is being used in the above pieces of code.
What I would like to be able to do, if at all possible, is rather than the 'id' value being shown in this form, I would like to convert it back to the appropriate text value. The text values are held in two separate tables, 'detectors' and 'searchheads' but I must admit, I'm really not sure where to begin.
I just wondered whether it would be at all possible please that someone cou开发者_运维百科ld show me what I need to do to show this information.
Many thanks and kind regards
Chris
Well, generally when you have a dropdown, each <option>
has a value=
and a content.
For example, you can retrieve the right list of ID and titles from the database, and the list should look as follows for example:
<select name="id">
<option value="1567">My City</option><!-- this is ID=1567 with title=My City-->
<option value="1322">Example City</option><!-- this is ID=1322 and title=Example City-->
</select>
The form, when submitted, returns the correct ID chosen from the dropdown and not its title. All you have to worry about is having an SQL query give you the list of locations with their IDs, and you just loop through them and make them generate a list with the above format. An example PHP & MySQL code would be simply:
<?php
$query = mysql_query("SELECT * FROM `saved_locations` WHERE `user` = '{$myUser}'");
echo '<select name="id">';
while ($row = mysql_fetch_object($query)) {
echo '
<option id="'.$row->id.'">'.$row->title.'</option>';
}
echo '
</select>';
?>
精彩评论