2011-08-27 100 views
0

我想知道是否有人可以帮助我。显示文本值而不是ID

我正在使用以下几段代码在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 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代码

<!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>&nbsp;</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>&nbsp;</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> 

我有顾虑我的两个领域的问题, 'detectorid' 和 'searchheadid'。这些字段的信息通过另一个表单上的两个下拉菜单保存。下拉菜单显示适合用户选择的文本值,但与每个选择相关联的“id”值保存到上述代码中使用的表中。

我希望能够做的,如果可能的话,而不是在这个表单中显示的'id'值,我想将它转换回适当的文本值。文本值分为两个独立的表格,'探测器'和'搜索头',但我必须承认,我真的不知道从哪里开始。

我只是想知道是否有可能请有人能告诉我我需要做什么才能显示这些信息。

许多的感谢和亲切的问候

克里斯

回答

0

好,通常当你有一个下拉菜单,每个<option>value=和内容。 例如,你可以从数据库中检索ID和标题的右侧列表,并将列表应如下所示例如:

<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> 

的形式,提交时,返回从下拉菜单中选择了正确的ID和不是它的头衔。 所有你必须担心的是有一个SQL查询给你的位置列表与他们的ID,你只是循环他们,让他们生成一个上面的格式的列表。 例如PHP MySQL的&代码将是简单的:

<?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>'; 
?> 
+0

你好,非常感谢来回抽出时间来回答我的职务。原谅我的问,但你能告诉我,你是否建议我改变如何创建下拉菜单?非常感谢和亲切的问候Chris – IRHM

+0

我明白了,我误解了你的问题。那么,最简​​单的方法就是添加一个隐藏的值:''并根据用户在找到匹配项后输入的内容进行更改。当从用户输入的内容中找到匹配项时,获取相关ID并将其保存在该字段中,然后可以使用它。 – casraf

+0

嗨,原谅我不能早日回复你,特别是当你有善意提供一些建议。请问您是否认为在加载表单时,有通过SQL执行此操作的方法。非常感谢和亲切的问候。克里斯 – IRHM

相关问题