[英]Getting database values in Javascript variable dynamically
I researched this subject many times but I could not find the right answer to my question. 我对此主题进行了很多次研究,但找不到正确的答案。 Let me explain it. 让我解释一下。
I'm creating an app with the Google Maps API where I want to have multiple locations shown on the map, based on my database values. 我正在使用Google Maps API创建一个应用,希望根据数据库值在地图上显示多个位置。 I'm having an object called Locations in my javascript, where I store the the variables 'name', 'lat' and 'lon' (latitude, longtitude). 我的javascript中有一个名为Locations的对象,在其中存储了变量“名称”,“纬度”和“ lon”(纬度,经度)。
var Locations =
[{
name: 'test',
lat: 52.351753,
lon: 5.002035
},
{
name: 'test2',
lat: 52.390839,
lon: 4.908722
}];
This part is where I store my locations. 这是我存储位置的地方。 Although this is working, it is hardcoded. 尽管此方法有效,但已进行了硬编码。 So, I want to get all my mysql database values out of the database into my javascript variable dynamically. 因此,我想将所有mysql数据库值从数据库中动态地获取到我的javascript变量中。 I foreach'd through the database entry's with PHP: 我用PHP遍历了数据库条目:
foreach ($query as $post)
{
?> <h1><?php echo $post['naam'] ?></h1>
<p><?php echo $post['plaats'] ?></p>
<p><?php echo $post['categorie'] ?></p>
<?php
}
Now I tried to get all of those values and put them into my javascript variable, but without any succes. 现在,我尝试获取所有这些值并将其放入我的javascript变量中,但没有成功。
var bedrijven = <?php echo json_encode($post); ?>;
When I console.log 'bedrijven' i'm only getting the last entry, but I want every row stored. 当我console.log'bedrijven'时,我只得到最后一个条目,但是我想存储每一行。 Can someone help me with this? 有人可以帮我弄这个吗? Would be really cool if it worked. 如果可以的话,真的很酷。
I hope I explained well enough. 我希望我解释得足够好。
Thanks! 谢谢!
$post
is your iteration variable, so it only contains one element of the array at a time. $post
是您的迭代变量,因此一次仅包含数组的一个元素。 If you want the entire array, assign that: 如果需要整个数组,请分配:
var bedrijven = <?php echo json_encode($query); ?>;
The examples on http://us1.php.net/json_encode should be helpful. http://us1.php.net/json_encode上的示例应该会有所帮助。 It looks like your $post should be an associative array: 看起来您的$ post应该是一个关联数组:
$post[] = ("name"=>"test","lat"=>52.351753, "lon"=> 5.002035); ?>
var bedrijven = <?php echo json_encode($post); ?>;
outputs: 输出:
var bedrijven = [{"name":"test","lat":52.351753,"lon":5.002035}]; var bedrijven = [{“名称”:“ test”,“ lat”:52.351753,“ lon”:5.002035}];
Try: 尝试:
PHP: PHP:
function getMarkers(){
$response = array();
foreach ($query as $post) {
$response[] = array(
'name' => $post['naam'],
'lat' => $post['plaats'],
'lng' => $post['categorie'],
);
}
echo json_encode($response);
}
then JS: 然后是JS:
function addMarkers(json){
var title =json[index]["naam"];
var lat =json[index]["plaats"];
var lng =json[index]["categorie"];
marker = new google.maps.Marker({
position: new google.maps.LatLng (lat, lng),
map: YOUR_MAP
});
}
jQuery.getJSON("URL OF PAGE",{
format: "json",
dataType: 'json',
contentType: "application/json; charset=utf-8"},
function(json){addMarkers(json);
});
better, to send the info with json and retrieve it with jQuery's getJSON
, this may need some tweaking.. But I have created what your talking about. 更好的是,使用json发送信息并使用jQuery的getJSON
检索信息,这可能需要一些调整。.但是我已经创建了您在说什么。 Shout if any further help is required. 如果需要任何进一步的帮助,请大喊大叫。
You May have to look into this a tiny bit to configure it.. 你可能要考虑这个一点点来配置它..
GOOD LUCK! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.