英文:
How to put JavaScript in PHP?
问题
我在做这个时遇到了困难,尝试了多次但仍然无法正常工作。我需要将名称、纬度和经度放入var = locations[];
。
这段代码的目的是在2D地图上显示地点的位置。
<div id="map" style="width: 1150px; height: 500px;"></div>
<?php
require 'dp.php';
$db = new DB();
$records = $db->shop_locat();
if(isset($records)) {
foreach($records as $row) {
echo '<script type="text/javascript"> var locations = [ ';
echo ' [ \''.$row['name'].'\' , '.$row['lat'].', '.$row['lng'].' ]; </script> ';
}
}
?>
<script type="text/javascript">
// 原始代码... var locations = [['Vista Mall Bataan', 14.655008481768526, 120.5338206800952],];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10.5,
center: new google.maps.LatLng(14.6417, 120.4818),
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>
dp.php
// 获取坐标
public function shop_locat() {
$qstr = "SELECT * FROM tbl_location";
$result = mysqli_query($this->link, $qstr);
$records = array();
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$records[] = [
'lat' => $row['lat'],
'lng' => $row['lng'],
'name' => $row['name'],
];
}
} else {
$records = null;
}
mysqli_free_result($result);
return $records;
}
我尝试了多种变体,但仍然无法正常工作。所以我希望有人可以帮助我。
这是我想要的输出,但名称、纬度和经度应该是这样的:
var locations = [['Vista Mall Bataan', 14.655008481768526, 120.5338206800952],];
在我的phpmyadmin中有地点的名称、纬度和经度,但当我尝试使用上述代码获取它时,没有输出。
英文:
I am having a hard tme doing this and I tried many attempts but it still won't work. I need to put the name, latitude and longitude in the var = locations[];
The purpose of this code is to show the locations of the places in a 2D Map
<div id="map" style="width: 1150px; height: 500px;"></div>
<?php
require 'dp.php';
$db = new DB();
$records = $db->shop_locat();
if(isset($records)) {
foreach($records as $row) {
echo '<script type="text/javascript"> var locations = [ ';
echo ' [ '.$row['name'].' , '.$row['lat'].', '.$row['lng'].' ]; </script> ';
}
}
?>
<script type="text/javascript">
// The original code... var locations = [['Vista Mall Bataan', 14.655008481768526, 120.5338206800952],];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10.5,
center: new google.maps.LatLng(14.6417, 120.4818),
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>
dp.php
// Get Coordinates
public function shop_locat() {
$qstr = "SELECT * FROM tbl_location";
$result = mysqli_query($this->link, $qstr);
$records = array();
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$records[] = [
'lat' => $row['lat'],
'lng' => $row['lng'],
'name' => $row['name'],
];
}
} else {
$records = null;
}
mysqli_free_result($result);
return $records;
}
I tried multiple variations of but it still won't work. So I am hoping that someone can help me.
This is the output that I want, but the name, latitude, and longitude is like this:
var locations = [['Vista Mall Bataan', 14.655008481768526, 120.5338206800952],];
The name, latitude, and longitude of the place is in my phpmyadmin, but when I try to get it, using my code above, there is no output
答案1
得分: 2
你可以按照所需的格式构建数组,然后将其转换为 JSON 格式,以便将其放入 JavaScript 中。手动逐个添加数组元素容易出错。
你可以尝试如下的方法:
<?php
$locations = array();
if(isset($records)) {
foreach($records as $row) {
array_push($locations, array($row['name'], $row['lat'], $row['lng']));
}
}
echo '<script type="text/javascript">';
echo 'var locations = '.json_encode($locations).';';
echo '</script>';
?>
英文:
You can build your array in the required format and json encode it so that you can put that in the javascript. Buliding the array manually by hand is error prone.
You can try something like below.
<?php
$locations = array();
if(isset($records)) {
foreach($records as $row) {
array_push($locations, array($row['name'], $row['lat'], $row['lng']));
}
}
echo '<script type="text/javascript">';
echo 'var locations = '.json_encode($locations).';';
echo '</script>';
?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论