2011-08-27 102 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

相關問題