【问题标题】:Show Text Value Instead Of ID显示文本值而不是 ID
【发布时间】:2011-11-05 04:35:27
【问题描述】:

我想知道是否有人可以帮助我。

我正在使用以下代码片段成功地在 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”值,而是将其转换回适当的文本值。文本值保存在两个单独的表中,“检测器”和“搜索头”,但我必须承认,我真的不知道从哪里开始。

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

非常感谢和亲切的问候

克里斯

【问题讨论】:

    标签: php javascript google-maps-markers


    【解决方案1】:

    好吧,通常当你有一个下拉菜单时,每个&lt;option&gt; 都有一个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>';
    ?>
    

    【讨论】:

    • 您好,非常感谢您抽出宝贵时间回复我的帖子。请原谅我的提问,但您能告诉我,您是否建议我更改下拉菜单的创建方式?非常感谢和亲切的问候克里斯
    • 我明白了,我误解了你的问题。好吧,最简单的方法是添加一个隐藏值:&lt;input type="hidden" name="id" value="123" /&gt;,并根据用户在找到匹配项后键入的内容进行更改。当从用户输入的内容中找到匹配项时,获取相关 ID 并将其保存在该字段中,然后您就可以使用它了。
    • 您好,请原谅我没有尽快回复您,尤其是当您好心提供一些建议时。请问您是否认为在加载表单时可以通过 SQL 执行此操作。非常感谢和亲切的问候。克里斯
    猜你喜欢
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多