这个位置变量在我的代码中工作正常。 http://vince.netau.net
var locations = [
["John Doe", "145 Rock Ridge Road, Chester, NY ", "41.314926,-74.270134", "http://maps.google.com/mapfiles/ms/icons/blue.png"],
["Jim Smith", "12 Williams Rd, Montvale, NJ ", "41.041599,-74.019554", "http://maps.google.com/mapfiles/ms/icons/green.png"],
["John Jones", "689 Fern St Township of Washington, NJ ", "40.997704,-74.050598", "http://maps.google.com/mapfiles/ms/icons/yellow.png"],
];
现在我的下一步是代替上面的静态数据,我想从我的mysql数据库中检索数据并将其用于var位置。我有这个php,它以与上面完全相同的格式从mysql中吐出数据。 http://vince.netau.net/db-connect-test.php
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, address, lat, lng, Icon FROM markers";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '["' . $row["name"]. '"'. ', '. '"' . $row["address"].'"'.', '. '"'. $row["lat"].','. $row["lng"].'"'.', '. '"'. $row["Icon"]. '"]'. ','. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
我只是不知道如何让它发挥作用。非常感谢有关如何执行此操作的任何帮助。我是初学者。
由于
更新 - 大卫,我尝试了你写的代码。在获取html中的位置数据方面,我还不确定这对我有什么作用?
<?php
//open connection to mysql db
$connection = mysqli_connect("","","","") or die("Error " . mysqli_error($connection));
//fetch table rows from mysql db
$sql = "select * from markers";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$location = array();;
if ($result->num_rows > 0) {
// output data of each row
$key = 0;
while($row = $result->fetch_assoc()) {
$locations[$key][] = $row["name"];
$locations[$key][] = $row["address"];
$locations[$key][] = $row["lat"];
$locations[$key][] = $row["lng"];
$locations[$key][] = $row["Icon"];
$key++;
}
}
else {
echo "0 results";
}
echo json_encode($locations);
//close the db connection
mysqli_close($connection);
?>
以下是结果,有点搞砸了。
[[&#34; John Doe&#34;,&#34; 147 Rock Ridge Road,Chester,NY&#34;,&#34; 41.314926&#34;,&#34; -74.270134&# 34;,&#34; http://maps.google.com/mapfiles/ms/icons/blue.png"],[&#34; Jim Smith&#34;,&#34; 14 Williams Rd, Montvale,NJ&#34;,#34; 41.041599&#34;,&#34; -74.019554&#34;,&#34; http://maps.google.com/mapfiles/ms/icons/green .png&#34;],[&#34; John Jones&#34;,&#34; 691 Fern St Township of Washington,NJ&#34;,&#34; 40.997704&#34;,&#34; - 74.050598&#34;&#34; HTTP://maps.google.com/mapfiles/ms/icons/yellow.png"]]
答案 0 :(得分:0)
在服务器端,您应该使用json_encode将数据插入页面。
e.g
<?php
echo "<script> var data = '" + json_encode($locations) + "'; </script>";
?>
在客户端,
var locations = JSON.parse(data);
答案 1 :(得分:0)
我看到你正在尝试获取位置数组,因此存储为multidimentional数组会很有帮助
$location = array();;
if ($result->num_rows > 0) {
$key = 0;
// output data of each row
while($row = $result->fetch_assoc()) {
$locations[$key][] = $row["name"];
$locations[$key][] = $row["address"];
$locations[$key][] = $row["lat"];
$locations[$key][] = $row["lng"];
$locations[$key][] = $row["Icon"];
$key++;
}
}
else {
echo "0 results";
}
//echo json_encode($locations);
<?php
echo "<script> var locations = '" . json_encode($locations) . "'; </script>";
?>
答案 2 :(得分:0)
因此,我们假设php文件现在为我提供了以前在html中var = locations下硬编码的确切位置数据。
我需要在html中使用什么新代码,以便调用php文件,硬编码数据将替换为从mysql表中提取的php位置结果。
谢谢