如何最好地将 mysql 数据实现到一行 javascript 代码中?

发布于 2024-11-29 03:21:28 字数 609 浏览 0 评论 0原文

我正在修改 Google 地图的一些 javascript,以便从我的数据库中检索不同的 pin 信息。如果我只用 php 来做这件事,它会是这样的:

<?php while($row = mysql_fetch_assoc($query))
{$title = $row['title']; $desc = $row['desc']; echo 'This '.$title .' and this '. $desc .';}?>

如果数据库中有 5 行,那么将有 5 行有 5 个不同的标题和描述。 但我有这行 javascript,我想在 while 循环中:

map.addMarkerByLatLng(37.8685, -1.5108, "Sanatario de Terburculosos", createInfo("title goes here",""));

我怎样才能编写它,以便该 javascript 位于 php while 循环中,或者我怎样才能执行 php while 循环的 javascript 等效项,以便我可以像上面一样,在自己的 javascript 代码中检索多行 lang、lat 信息?

谢谢。

I am modifying some javascript for Google maps so that the different pin information is retrieved from my database. If I was to do this with just php it would be like this:

<?php while($row = mysql_fetch_assoc($query))
{$title = $row['title']; $desc = $row['desc']; echo 'This '.$title .' and this '. $desc .';}?>

If there were 5 rows in the database there will be five lines with 5 different titles and descriptions.
But I have this line of javascript that I would like to be in a while loop:

map.addMarkerByLatLng(37.8685, -1.5108, "Sanatario de Terburculosos", createInfo("title goes here",""));

How can i write it so that that javascript is in a php while loop or how can i do the javascript equivelant of the php while loop so that i can retrieve multiple rows of lang,lat info each in its own javascript code like above?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

趁年轻赶紧闹 2024-12-06 03:21:29

ajax.php

<?php
    $obj = array();
    while($row = mysql_fetch_assoc($query)){
        $obj[] = new array(
            "title" => $row["title"],
            "desc"  => $row["desc"],
            "lat"   => $row["lat"],
            "lon"   => $row["lon"]
        );
    }
    echo json_encode($obj);
?>

jquery ajax

$.getJSON("ajax.php",function(data){
    for(var i=0;i<data.length;i++){
        map.addMarkerByLatLng(
            data[i].lon, 
            data[i].lat, 
            data[i].description, 
            createInfo(data[i].title,""));
    }
});

ajax.php

<?php
    $obj = array();
    while($row = mysql_fetch_assoc($query)){
        $obj[] = new array(
            "title" => $row["title"],
            "desc"  => $row["desc"],
            "lat"   => $row["lat"],
            "lon"   => $row["lon"]
        );
    }
    echo json_encode($obj);
?>

jquery ajax

$.getJSON("ajax.php",function(data){
    for(var i=0;i<data.length;i++){
        map.addMarkerByLatLng(
            data[i].lon, 
            data[i].lat, 
            data[i].description, 
            createInfo(data[i].title,""));
    }
});
风和你 2024-12-06 03:21:29

结合您提供的两个代码示例:

<?php
$pinTpl = 'map.addMarkerByLatLng( %s , %s , "%s" , createInfo( "%s" , "" ) );';
while( $row = mysql_fetch_assoc( $query ) ){
  echo sprintf( $pinTpl ,
         $row['lat'] ,   # Where 'lat' is the field name for the latitude in DB
         $row['long'] ,  # Where 'long' is the longitude field name in DB
         $row['title'] , # The title
         $row['desc']    # The description
  )."\n";                # A newline, for readability
}
?>

Combine the two code examples you have provided:

<?php
$pinTpl = 'map.addMarkerByLatLng( %s , %s , "%s" , createInfo( "%s" , "" ) );';
while( $row = mysql_fetch_assoc( $query ) ){
  echo sprintf( $pinTpl ,
         $row['lat'] ,   # Where 'lat' is the field name for the latitude in DB
         $row['long'] ,  # Where 'long' is the longitude field name in DB
         $row['title'] , # The title
         $row['desc']    # The description
  )."\n";                # A newline, for readability
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文