I wonder whether someone may be able to help me please.
I am using the code below to correctly show markers for all the locations stored in mySQL database.
PHP
<?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
Amended Code
$query = "select l.locationname, l.address, l.osgb36lat, l.osgb36lon, count(*) as totalfinds from locations as l left join finds as f on l.locationid=f.locationid";
$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("locationname",$row['locationname']);
$newnode->setAttribute("address",$row['address']);
$newnode->setAttribute("osgb36lat",$row['osgb36lat']);
$newnode->setAttribute("osgb36lon",$row['osgb36lon']);
$newnode->setAttribute("finds",$row['finds']);
$newnode->setAttribute("totalfinds",$row['totalfinds']);
}
}
echo $dom->saveXML();
?>
HTML
<!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>Locations</title>
<link rel="stylesheet" href="css/alllocationsstyle.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 = {
0: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
1: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_green.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(54.312195845815246,-4.45948481875007),
zoom:6,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
// 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");
for (var i = 0; i < markers.length; i++) {
var locationname = markers[i].getAttribute("locationname");
var address = markers[i].getAttribute("address");
var finds = markers[i].getAttribute("finds");
var totalfinds = markers[i].getAttribute("totalfinds");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("osgb36lat")),
parseFloat(markers[i].getAttribute("osgb36lon")));
var html = "<b>" + locationname + "</b>";
var icon = customIcons[finds] || {};
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"></div>
</body>
</html>
What I would like to do is to adapt the coding whereby the ‘Total number of finds’ for each location is shown along with the ‘Location Name’ in the Infowindow that is created for each marker.
I know that I need to get this information from my table called ‘finds’ where I need to count the number of rows where the ‘locationid’ matches the one in the ‘locations’ table, but I must admit I haven’t a clue how to do this.
I just wondered whether someone could perhaps please provide some guidance on what I need to do to achieve this.
Many thanks and kind regards
Chris
Finds SQL Dump
--
-- Table structure for table `finds`
--
DROP TABLE IF EXISTS `finds`;
CREATE TABLE IF NOT EXISTS `finds` (
`userid` int(6) NOT NULL,
`locationid` int(6) NOT NULL,
`findid` int(6) NOT NULL auto_increment,
`findosgb36lat` float(10,6) NOT NULL,
`findosgb36lon` float(10,6) NOT NULL,
`dateoftrip` varchar(8) NOT NULL,
`findname` varchar(35) NOT NULL,
`finddescription` varchar(100) NOT NULL,
`findimage` varchar(200) default NULL,
`additionalcomments` varchar(600) default NULL,
`makepublic` varchar(3) NOT NULL default 'no',
PRIMARY KEY (`findid`)
) ENGINE=MyISAM AUTO_INCREMENT=34 DEFAULT CHARSET=utf8 AUTO_INCREMENT=34 ;
Locations SQL dump
--
-- Table structure for table `locations`
--
CREATE TABLE `locations` (
`userid` int(6) NOT NULL,
`locationid` int(6) NOT NULL auto_increment,
`locationname` varchar(80) NOT NULL,
`address` varchar(110) NOT NULL,
`osgb36lat` float(10,6) NOT NULL,
`osgb36lon` float(10,6) NOT NULL,
`osgridref` varchar(20) NOT NULL,
`wgs84latd` int(2) NOT NULL,
`wgs84latm` int(2) NOT NULL,
`wgs84lats` decimal(6,2) NOT NULL,
`wgs84latb` varchar(1) NOT NULL,
`wgs84lond` int(2) NOT NULL,
`wgs84lonm` int(2) NOT NULL,
`wgs84lons` decimal(6,2) NOT NULL,
`wgs84lonb` varchar(1) NOT NULL,
`nameoflocationcontact` varchar(30) NOT NULL,
`locationcontactsaddressline1` varchar(50) NOT NULL,
`locationcontactsaddressline2` varchar(50) default NULL,
`locationcontactsaddressline3` varchar(50) default NULL,
`locationcontactsaddressline4` varchar(50) default NULL,
`locationcontactstelephonenumber` varchar(15) default NULL,
`finds` int(1) NOT NULL,
PRIMARY KEY (`locationid`)
) ENGINE=MyISAM AUTO_INCREMENT=27 DEFAULT CHARSET=utf8 AUTO_INCREMENT=27 ;
You’d want to change the sql fetching code to join the finds table. Left join seems suitable for this.
Updated sql code:
You will now be able to get the total number of location finds in the total_finds element of the $row array. Also note I removed the where 1 condition in the original sql as it makes no sense.
Of course you’ll also need save the total_finds value in the XML and make the appropriate changes in the JS code.