我想知道是否有人可以帮助我。
我正在使用下面的代码正确显示存储在mySQL数据库中的所有位置的标记。
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
修订后的代码
$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>
我想做的是调整编码,以便显示每个位置的“查找总数”以及为每个标记创建的Infowindow中的“位置名称”。
我知道我需要从我的表中获取名为'finds'的信息,我需要计算'locationid'匹配'locations'表中的行数,但我必须承认我没有'了解如何做到这一点。
我只是想知道某人是否也可以就我为实现这一目标需要做些什么提供一些指导。
非常感谢和亲切的问候
克里斯
查找SQL转储
--
-- 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 ;
地点SQL转储
--
-- 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 ;
答案 0 :(得分:0)
您想要更改sql获取代码以加入查找表。左连接似乎适合这个。
更新了sql代码:
选择l.locationid,f.locationid,l.locationname,l.address, l.osgb36lat,l.osgb36lon,l.finds,count(f.locationid)as totalfinds 来自左边连接的位置在l.locationid = f.locationid上找到f l.locationid分组
现在,您可以在$ row数组的total_finds元素中获取位置查找的总数。另请注意,我删除了原始sql中的 where 1 条件,因为它没有任何意义。
当然,您还需要在XML中保存total_finds值,并在JS代码中进行适当的更改。