gpt4 book ai didi

javascript - 如何将数组值从 PHP 传递到 JavaScript?

转载 作者:行者123 更新时间:2023-12-02 15:03:40 25 4
gpt4 key购买 nike

好的。所以我一整天都在这里,阅读并遵循有关该主题的所有解决方案,但我一无所获。什么都不起作用。您能用完整且易于理解的示例向我展示如何做到这一点吗?我对 JavaScript 和 PHP 很陌生,那么如何将 PHP 文件中的数组值获取到 JavaScript。我尝试这样做:var locations = <?php echo json_encode($location); ?>;但我给了我错误。但没有人回答为什么。这段代码在这里:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<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 locations = [
['Municipal Hall', 6.414333734068895, 125.61093270778656, 1],
['Camarillo Household', 6.4345278, 125.58975, 2],
['Perez Household', 6.4343889, 125.59202777777777, 3],
['Usman Household', 6.4338056, 125.59191666666666, 4],
['Lim Household', 6.4333889, 125.59419444444444, 5]
];

var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(6.40, 125.60),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var marker, i;

for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});

google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>
</body>
</html>

我想更改变量locations的值来 self 的数据库的值。所以我得到了这个 PHP 文件:

<?php
$servername = "host";
$username = "username";
$password = "password";
$dbname = "dbname";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql='SELECT a.Household_Name, b.Latitude, b.Longitude FROM household a, location b WHERE a.Household_ID = b.Household_ID;';
$result = mysqli_query($conn, $sql);

if(mysqli_num_rows($result) > 0){
// output data of each row

for($i=0;$i<mysqli_num_rows ( $result );$i++){
$row=mysqli_fetch_row($result);

$location[]= array($row[0].', '.$row[1].', '.$row[2].','.($i+1));
//echo "Household Name: " . $row[0]. " - Latitude: " . $row[1]. " - Longitude " . $row[2]. " " .($i+1)."<br>";
}

}else{echo "0 results";}
?>

效果很好。它从数据库输出数据。现在我想知道的是如何将该值从数据库转换为我可以用来代替 locations 的值变量以便标记出现在 map 上? var locations = <?php echo json_encode($location); ?>;这家伙给了我错误,我遵循了我能做到的每一条指示,但仍然是错误。您可以修改我的代码以使其正常工作吗?或者您可以在您所知的工作/功能代码中指出我吗?请帮我。我这里遇到了大麻烦。

最佳答案

尝试更改 location[] 变量:

$location[]= array($row[0].', '.$row[1].', '.$row[2].','.($i+1));

$location[]= array($row[0],$row[1],$row[2],($i+1));

我在下面添加了完整的代码及其工作原理:

<?php
$servername = "host";
$username = "username";
$password = "password";
$dbname = "dbname";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql='SELECT a.Household_Name, b.Latitude, b.Longitude FROM household a, location b WHERE a.Household_ID = b.Household_ID;';
$result = mysqli_query($conn, $sql);

if(mysqli_num_rows($result) > 0){
// output data of each row

for($i=0;$i<mysqli_num_rows ( $result );$i++){
$row=mysqli_fetch_row($result);

$location[]= array($row[0],$row[1],$row[2],($i+1));
//echo "Household Name: " . $row[0]. " - Latitude: " . $row[1]. " - Longitude " . $row[2]. " " .($i+1)."<br>";
}
}else{echo "0 results";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<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 locations = [
['Municipal Hall', 6.414333734068895, 125.61093270778656, 1],
['Camarillo Household', 6.4345278, 125.58975, 2],
['Perez Household', 6.4343889, 125.59202777777777, 3],
['Usman Household', 6.4338056, 125.59191666666666, 4],
['Lim Household', 6.4333889, 125.59419444444444, 5]
];*/
var locations = <?php echo json_encode($location); ?>;


var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(6.40, 125.60),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var marker, i;

for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});

google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>
</body>
</html>

关于javascript - 如何将数组值从 PHP 传递到 JavaScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35265334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com