PHP 脚本仅从 MySQL 数据库中获取地址字段的第一行
PHP Script fetching only first row of address field from the MySQL database
我正在构建一个页面,其中从数据库中获取地址并显示所有位置,但代码仅显示数据库中的第一个地址字段。我什至搜索了谷歌和所有相关的问题,但似乎没有任何效果。
作为输出,我在地址字段中获得了第一个位置,并且该位置显示在谷歌地图上。我需要做的是从数据库中的地址字段中获取所有地址行,并将它们显示在Google地图(多个标记)上。
有什么方法可以让我获取所有地址并将它们存储在数据库中的数组中,并在谷歌地图上显示来自" var地址"的地址。
任何机构都可以帮助我是这个概念的新手。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<?
error_reporting(0);
include('dbcon.php');
$result = mysql_query("SELECT address FROM markers");
$new_array = array();
while ($row = mysql_fetch_assoc($result)) {
$new_array[] = $row['address'];
}
$add_js = json_encode( $new_array );
?>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 400px;"></div>
<script type="text/javascript">
var side_bar_html = "";
var icon1 = "prop.png";
var icon2 = "build.png";
var locations = <?php echo $add_js ?>;
//alert(locations);
function geocodeAddress(i) {
geocoder.geocode(
{
'address' : locations[i]
},
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
createMarker(results[0].geometry.location, i);
} else {
alert('Geocode was not successful for the
following reason: '
+ status);
}
});
}
function createMarker(latlng,i) {
var marker = new google.maps.Marker({
map : map,
icon : icon1,
position : latlng
});
//add info window
google.maps.event.addListener(marker, 'mouseover', function() {
marker.setIcon(icon2);
infowindow.setContent(locations[i]);
infowindow.open(map, marker);
title: 'Property!'
});
google.maps.event.addListener(marker, 'mouseout', function() {
marker.setIcon(icon1);
infowindow.setContent(locations[i]);
infowindow.close(map, marker);
title: 'Property!'
});
//end of adding info window
return marker;
}
var map = new google.maps.Map(document.getElementById('map'), {
zoom : 10,
center : new google.maps.LatLng(28.6139, 77.2089),
mapTypeId : google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow({
size: new google.maps.Size(150,50)});
var geocoder = new google.maps.Geocoder();
for (var i = 0; i < locations.length; i++) {
geocodeAddress(i);
}//end of for loop
</script>
</body>
</html>
行
while($add[]=mysql_fetch_row($sql));
从 SQL 结果中获取尽可能多的行并将它们放入数组中;您将获得一个列数组数组。
PHP 中的数组没有内置方法来自动将它们转换为字符串,这就是<?=$add?>
行上会发生的情况。您需要遍历它们并单独打印数组中的每个项目,或使用 implode
等方法将它们转换为字符串。
我怀疑您要做的是使用查询从数据库中获取单个地址,例如
SELECT address FROM markers LIMIT 1
并执行以下操作:
$sql=(mysql_query("SELECT address FROM markers LIMIT 1"));
$addressParts = mysql_fetch_row($sql);
$add = ($addressParts && count($addressParts)) ? $addressParts[0] : '';
这可能会实现你想要实现的目标 - 从数据库中加载一个地址并将其注入到你的JavaScript中。
相关文章:
- 通过单击动态加载的表中同一行的另一个字段来更新一行的字段
- 我可以在Dynamics CRM 2016的复合地址字段中添加两个选项字段吗
- 如何将可拖动的谷歌地图标记的地址传递到输入字段中
- 当字段为空时,如何省略一行(使用胡子/把手)
- I'm试图创建一个html表单,根据字段的输入生成url地址
- 如何使用LinkedIn Javascript API获取电子邮件地址字段
- PHP 脚本仅从 MySQL 数据库中获取地址字段的第一行
- mailto:新窗口,但“收件人”字段地址 mailto:user@test.com
- 使用 javascript 验证地址字段
- 尝试检测网格视图的哪一行处于编辑模式,然后在选中复选框字段时调整文本框
- 如果我在地址数组中设置了多个地址对象,那么是否有必要为两个字段提供地址
- 如何为一行文本输入字段添加用户可编辑的文本标签
- 向PHP表单添加更多字段(用户可以添加多个地址)
- 使用Drag-able Google Maps API V3获取输入字段中的标记地址
- 如何禁用除地址字段以外的所有字段的空格栏
- 在更新地址字段时导航到新内容而不重新加载页面
- 谷歌地图API自动完成同一页面上的第二个地址字段
- 复制账单和送货地址字段复制到另一个
- 复选框切换到设置地址字段
- 在CRM 2011中查找自定义实体上的地址字段