[英]PHP Display multi-dimentional array value on google map
Can anyone help me, I am very new to PHP, maybe the answer was very obvious. 谁能帮我,我对PHP还是很陌生,也许答案很明显。
I store users details and postcodes in the database, then I need to display the postcodes as markers on the google map using javascript (this was working). 我将用户详细信息和邮政编码存储在数据库中,然后需要使用javascript在Google地图上将这些邮政编码显示为标记(这是可行的)。
But I was trying to display each user data on google map's info window, I am only be able to display the last row of the result, so all info windows were display the same record, not the individual data to that postcode. 但是我试图在google map的信息窗口上显示每个用户数据,但是我只能显示结果的最后一行,因此所有信息窗口都显示相同的记录,而不是该邮政编码的单个数据。
I think I need to combine below two queries, but I don't know how. 我想我需要结合以下两个查询,但是我不知道如何。 Because the postcode need to be implode to get "postcode" to show on map. 因为需要对邮政编码进行内爆才能使“邮政编码”显示在地图上。
What I am trying to achieve: 我想要达到的目标:
postcode need to be ("AA4 1DD", "AB1 5CD","CC4 1BBs");
infowindow (Fred, 7 street, AA4 1DD);
What I have in the Database: 我在数据库中拥有什么:
name Fred Ann John
address 7 street 8 road 4 line
postcode AA4 1DD AB1 5CD CC4 1BB
PHP: PHP:
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$locations = $conn->prepare("SELECT postcode FROM tbl_data");
$locations->execute();
$lo= $locations->fetchAll(PDO::FETCH_COLUMN);
echo "'". $t = implode(' ,', $lo)."'";// working
//info window
$info_stmt = $conn->prepare("SELECT name, postcode FROM tbl_data, tbl_login WHERE tbl_data.user_id=tbl_login.user_id ");
$info_stmt->execute();
$result = $info_stmt -> fetchAll();
foreach( $result as $row ) {
echo $row[0];
/* echo "<pre>";
echo gettype($row[0]);
echo "</pre>";*/
}
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
die();
}
Javascript: 使用Javascript:
var addressArray = new Array(<?php echo "'". $t = implode("' ,'", $lo)."'"; ?>); //postcodes working
var geocoder = new google.maps.Geocoder();
for (var i = 0; i < addressArray.length; i++) {
geocoder.geocode( { 'address': addressArray[i]}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title: 'Click for more details'
});
//create info window
var infoWindow = new google.maps.InfoWindow({
content: "<p>Name: </p><?php echo $row[0].'<br />'.$row[1]; ?>"
});
Your issue is here: 您的问题在这里:
content: "<p>Food Name: </p><?php echo $row[0].'<br />'.$row[1]; ?>"
You cannot mix JavaScript and PHP like this, and $row does not - or shouldn't - actually exist in this context due to it not being within the foreach
statement or - if this is a seperate JS file - you have no corresponding context. 您不能像这样混合使用JavaScript和PHP,并且$ row不在-或不应该在该上下文中实际存在,因为它不在foreach
语句之内,或者-如果这是一个单独的JS文件-您没有相应的上下文。
To achieve what you need, I'd suggest using AJAX and setting a GET
request to a file which then returns an array which you can then use in your JavaScript. 为了实现您的需求,我建议使用AJAX并将GET
请求设置到文件,然后该文件返回一个数组,然后可以在JavaScript中使用该数组。
You can read documentation on this here . 您可以在此处阅读有关文档。
Note, $row
should be used like this: $row['ColumnName']
(not just for readability). 请注意, $row
应该这样使用: $row['ColumnName']
不只是可读性)。
For example: echo $row['address'];
例如: echo $row['address'];
Example Code (PHP): 示例代码(PHP):
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$locations = $conn->prepare("SELECT postcode FROM tbl_data");
$locations->execute();
$lo= $locations->fetchAll(PDO::FETCH_COLUMN);
echo "'". $t = implode(' ,', $lo)."'";// working
//info window
$info_stmt = $conn->prepare("SELECT name, postcode FROM tbl_data, tbl_login WHERE tbl_data.user_id=tbl_login.user_id ");
$info_stmt->execute();
echo $info_stmt->fetchAll();
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
Then your JavaScript: 然后你的JavaScript:
$(document).ready(function () {
var output[];
$.get("phpfile.php")
.done(function(data) {
output = data;
});
});
output
is now the array of all your results
and can be used. output
现在是所有results
的数组,可以使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.