如何在PHP中嵌入JavaScript?

huangapple go评论114阅读模式
英文:

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

&lt;div id=&quot;map&quot; style=&quot;width: 1150px; height: 500px;&quot;&gt;&lt;/div&gt;

&lt;?php  
require &#39;dp.php&#39;;
$db = new DB();

$records = $db-&gt;shop_locat();
if(isset($records)) {
    foreach($records as $row) {
        echo &#39;&lt;script type=&quot;text/javascript&quot;&gt; var locations = [ &#39;;
		echo &#39; [ &#39;.$row[&#39;name&#39;].&#39; , &#39;.$row[&#39;lat&#39;].&#39;, &#39;.$row[&#39;lng&#39;].&#39; ]; &lt;/script&gt; &#39;;
	}
}
?&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
// The original code...	var locations = [[&#39;Vista Mall Bataan&#39;, 14.655008481768526, 120.5338206800952],];

var map = new google.maps.Map(document.getElementById(&#39;map&#39;), {
    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 &lt; 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, &#39;click&#39;, (function(marker, i) {
	    return function() {
	        infowindow.setContent(locations[i][0]);
	        infowindow.open(map, marker);
	    }
    })(marker, i));
}
&lt;/script&gt;

dp.php

// Get Coordinates
    public function shop_locat() {
        $qstr = &quot;SELECT * FROM tbl_location&quot;;
        $result = mysqli_query($this-&gt;link, $qstr);
        $records = array();

        if (mysqli_num_rows($result) &gt; 0) {
            while ($row = mysqli_fetch_assoc($result)) {
                $records[] = [
                    &#39;lat&#39; =&gt; $row[&#39;lat&#39;],
                    &#39;lng&#39; =&gt; $row[&#39;lng&#39;],
                    &#39;name&#39; =&gt; $row[&#39;name&#39;],
                    
                    
                ];
            }
        } 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 = [[&#39;Vista Mall Bataan&#39;, 14.655008481768526, 120.5338206800952],];

如何在PHP中嵌入JavaScript?

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.

&lt;?php
$locations = array();
if(isset($records)) {
	foreach($records as $row) {
    	array_push($locations, array($row[&#39;name&#39;], $row[&#39;lat&#39;], $row[&#39;lng&#39;]));
    }
}

echo &#39;&lt;script type=&quot;text/javascript&quot;&gt;&#39;;
echo &#39;var locations = &#39;.json_encode($locations).&#39;;&#39;;
echo &#39;&lt;/script&gt;&#39;;
?&gt;

huangapple
  • 本文由 发表于 2023年6月8日 20:37:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定