使用php填充javascript数组 [英] Use php to populate javascript array

查看:83
本文介绍了使用php填充javascript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,
我有以下代码:

All, I have the following bit of code:

function addPoints() {

newpoints[0] = new Array(41.45998, 87.59643, icon0, 'Place', 'Content to open'); 

    for(var i = 0; i < newpoints.length; i++) {
       var point = new GPoint(newpoints[i][1],newpoints[i][0]);
       var popuphtml = newpoints[i][4] ;
       var marker = createMarker(point,newpoints[i][2],popuphtml);
       map.addOverlay(marker);
   }
}

此处还有其他代码可以显示标记我的地图。但是这个值是硬编码的。我有一个PHP / mySQL数据库,其中包含纬度/经度坐标以及其他一些值。假设我有三个要为其创建标记的条目。我如何将addPoints函数传递给我从数据库中获取的lat / long,以便我可以正确使用此函数?

There is other code around this to display the marker on my map. However this value is hardcoded. I have a PHP/mySQL database that has lat/long coordinates along with some other values. Say I have like three entries that I want to create markers for. How would I pass the addPoints function the lat/long that I got from my database so I can use it in this function correctly?

我更新了我的代码看起来像以下为addPoints:

I updated my code to look like the following for the addPoints:

function addPoints(num, lat, long) {
newpoints[num] = new Array(lat, long, icon0, 'Place', 'Stuff name'); 
alert("The newpoints length is: "+newpoints.length);
for(var i = 1; i < newpoints.length; i++) {
    var point = new GPoint(newpoints[i][1],newpoints[i][0]);
    var popuphtml = newpoints[i][4] ;
    var marker = createMarker(point,newpoints[i][2],popuphtml);
    map.addOverlay(marker);
}
}

我通过以下方式调用此函数:

I call this function by doing this:

<script> 
    addPoints('<?php echo json_encode($num_coordinates); ?>','<?php echo json_encode($lat_coordinates); ?>', '<?php echo json_encode($long_coordinates); ?>');
</script>

虽然不起作用。当我尝试不将它传递给javascript并输出lat坐标时。我得到以下输出:

It doesn't work though. When I try not to pass it to javascript and just output the lat coordinates for example. I get the following output:

{"1":"40.59479899","2":"41.4599860"}

我的数组中哪个是正确的坐标。虽然没有创建标记。关于下一步该怎么做或者我做错了什么的想法?

Which are the correct coordinates in my array. No markers get created though. Any ideas on what to do next or what I'm doing wrong?

推荐答案

PHP在服务器上执行之后才被发送到客户端。因此,如果你能做到这样的事情:

PHP executes on the server before getting sent to the the client. Therefor, if you can do things like this:

newpoints [0] = new Array(<?php echo $ lattitude;?> ,<?php echo $ longitude;?>,icon0,'Place','打开内容');

其中 $ lattitude $ longitude 是您使用PHP从数据库中提取的值。

Where $lattitude and $longitude are values that you pulled out of you database with PHP.

当客户端请求此页面时,您的PHP代码会执行,实际值会插入到这些php标记使其看起来像您提供的示例的位置,然后它会被发送到客户端。

When this page is requested by the client, your php code executes, real values get plugged in where those php tags are making it look like the example you provided, and then it gets sent to the client.

如果你想在客户端使用JS更改这些值,或者从服务器获取新值,请告诉我,我将添加一个例子。

If you want to change these values using JS on the client, or fetch new ones from the server, let me know and I'll add an example of that.

编辑:

好的,根据您的评论,听起来像你有几个选择。这是一个:

Okay, in light of your comments, it sounds like you've got a few options. Here's one:

当用户选择一个类别(餐馆,酒吧等)时,您将该类别作为url参数传递并重新加载整个页面,或者仅重新加载地图它的一部分(取决于你的设置,但可能值得调查)。您的链接看起来像这样:

When the user selects a category (restaurants, bars, etc) you pass that category as a url parameter and reload either the whole page, or just the map part of it (depends on your set up but might be worth investigating). Your link would look something like this:

http://www.your-domain-here.com/maps.php?category=bars

Maps.php已准备好使用 $ _ GET 数组捕获该类别:

Maps.php is ready to catch the category using the $_GET array:

$ category = $ _GET ['category']; //'bars'

然后你的php从数据库中抓取相应的位置数据(我会把那部分留给你)并坚持下去它在JS控制的地图能够使用的变量中:

Your php then grabs the appropriate location data from the database (I'll leave that part to you) and sticks it in a variable that your JS-controlled map will be able to use:

//JS in maps.php  -  you could add this var to the window object
// if you have separated js files...
var locationCoords = <?php echo json_encode($arrayOfCoordinatesFromDB);?>;

当您在客户端计算机上加载页面时,它现在有一个用于地图的坐标数组准备进入 locationCoords 变量。

When you page loads on the client machine, it now has an array of coordinates to use for the map ready to go in the locationCoords variable.

然后,根据您需要在地图上显示的坐标,你使用标准Javascript将它们作为参数传递给你的 addPoints()(这里没什么棘手的)。

Then, depending on which coordinates you need to display on the map, you pass them as arguments to your addPoints() using standard Javascript (nothing tricky here).

我就是这样做的。希望有所帮助!

That's how I'd do it. Hope that helps!

这篇关于使用php填充javascript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆