2011-08-13 20 views
0

我想知道是否有人能夠幫助我。在窗口中顯示'Total Of'

我使用下面的代碼來正確顯示存儲在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> 

我想這樣做的是ADAP t代碼顯示每個位置的「查找總數」以及爲每個標記創建的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

你會想更改sql讀取代碼以加入查找表。左連接似乎適合這一點。

更新的SQL代碼:

選擇l.locationid,f.locationid,l.locationname,l.address, l.osgb36lat,l.osgb36lon,l.finds,計數(F。 locationid)從爲L左側位置totalfinds 加入的發現爲f上l.locationid = f.locationid 組由l.locationid

現在,您將能夠得到位置的總數發現在total_finds $ row數組的元素。另外請注意,我刪除了原始sql中的其中1條件,因爲它沒有任何意義。

當然,您還需要保存XML中的total_finds值並在JS代碼中進行相應的更改。

+0

嗨,非常感謝您花時間回覆我的帖子。我已經做出了您所建議的更改,但不幸的是我似乎無法使其發揮作用。現在地圖只顯示錶格中第一條記錄的標記。沒有顯示錯誤,地圖按預期打開。你能告訴我,我哪裏可能會出錯。我已經更新了我的原始位置,顯示了新的代碼。很多感謝和問候。 Chris – IRHM

+0

嗨,我試圖重新創建這個,並通過使用測試輸入,我設法至少設置了兩個標記。我不確定從哪裏出發,你確定所有來自數據庫的數據都是正確的嗎?如果您希望我更準確地重新創建數據庫轉儲將會有所幫助。 – kjetilh

+0

嗨,首先非常感謝你對我的支持,非常感謝。我已經回到桌子上了,對我來說一切似乎都沒問題,但我對此很新,所以我可能是錯的。我已將sql轉儲附加到我的原始文章。非常感謝和親切的問候。 Chris – IRHM