douhao2026 2017-01-13 06:42
浏览 38
已采纳

使用php将地址从mysql DB读入数组进行地理编码

<?php  
$query = mysqli_query($connection, "SELECT address1, address2, parish FROM `contestantsaddress`");
    //Starts while loop so all addresses for the given information will be populated.
    $addresses = array();
    while($result = mysqli_fetch_array($query)) //instantiates array
    {
            $address1 = $result['address1'];
            $address2 = $result['address2'];
            $parish = $result['parish'];
            //$country = $result['country'];

        array_push($addresses1,$address1, $address2, $parish, $country);
        $count++;
    } //ends while


?>

for(var i=0; i<10; i++){
var addresses = [<?php echo $addresses; ?>];
}

//I would want the above addresses to be populated in the array just as the one below so I can geocode them. The problem is I try to achieve this using php to push them in the array to have them as the one below but I can't seem to get it working. I am using php and javascript to achieve this.

// ======= An array of locations that we want to Geocode ========

var addresses = [
               '251 Pantigo Road Hampton Bays NY 11946',
               'Amagensett Quiogue NY 11978',
               '789 Main Street Hampton Bays NY 11946',
               '30 Abrahams Path Hampton Bays NY 11946',
               '3 Winnebogue Ln Westhampton NY 11977',
               '44 White Oak Lane Montauk NY 11954',
               '107 stoney hill road Bridgehampton NY 11932',
               '250 Pantigo Rd Hampton Bays NY 11946',
               '250 Pantigo Rd Hampton Bays NY 11946',
               '44 Woodruff Lane Wainscott NY 11975',
               'Address East Hampton NY 11937',
               'Address Amagansett NY 11930',
               'Address Remsenburg NY 11960 ',
               'Address Westhampton NY 11977',
               'prop address Westhampton Dunes NY 11978',
               'prop address East Hampton NY 11937',
               'Address East Hampton NY 11937',
               'Address Southampton NY 11968',
               'Address Bridgehampton NY 11932',
               'Address Sagaponack NY 11962',
                "A totally bogus address"
      ];
  • 写回答

1条回答 默认 最新

  • duanhuanzhi6431 2017-01-13 07:53
    关注

    Just to show you the power of PDO, which in many ways is better than mysqli

    <?php  
    $sql = "SELECT CONCAT_WS(' ',address1, address2, parish) FROM contestantsaddress";
    $addresses= json_encode($pdo->query($sql)->fetchAll(PDO::FETCH_COLUMN));
    ?>
    var addresses = <?=$addresses?>;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角