1

我正在構建一個頁面,其中地址從數據庫中提取並顯示所有位置,但代碼僅顯示數據庫中的第一個地址字段。 我甚至搜索谷歌,和所有相關的問題,但似乎沒有工作。PHP腳本只提取MySQL數據庫中的第一行地址字段

作爲輸出,我在地址欄中獲得第一個位置,並且該位置正在谷歌地圖上顯示。 我需要做的是從數據庫中的地址字段獲取所有地址行,並在谷歌地圖(多個標記)上顯示它們。

有什麼辦法讓我可以獲取所有的地址,並將它們存儲在數據庫的數組中,並顯示谷歌地圖上'var address'的地址。

任何機構請幫助我是這個概念的新手。

 <!DOCTYPE html> 
     <html> 
     <head> 
     <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
     <title>Google Maps Multiple Markers</title> 
     <? 
     error_reporting(0); 
    include('dbcon.php'); 
    $result = mysql_query("SELECT address FROM markers"); 
    $new_array = array(); 
    while ($row = mysql_fetch_assoc($result)) { 
    $new_array[] = $row['address']; 
    } 

    $add_js = json_encode($new_array); 
    ?> 
    <script 
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> 
    <script src="http://maps.google.com/maps/api/js?sensor=false" 
    type="text/javascript"></script> 
    </head> 
    <body> 
    <div id="map" style="width: 500px; height: 400px;"></div> 
    <script type="text/javascript"> 
    var side_bar_html = ""; 
    var icon1 = "prop.png"; 
     var icon2 = "build.png"; 
     var locations = <?php echo $add_js ?>; 
    //alert(locations);   
    function geocodeAddress(i) { 
      geocoder.geocode(
         { 
          'address' : locations[i] 
         }, 
         function(results, status) { 
          if (status == google.maps.GeocoderStatus.OK) { 
           map.setCenter(results[0].geometry.location); 
           createMarker(results[0].geometry.location, i); 
          } else { 
           alert('Geocode was not successful for the 
           following reason: ' 
             + status); 
          } 
         }); 
         } 

     function createMarker(latlng,i) { 
     var marker = new google.maps.Marker({ 
            map : map, 
        icon : icon1, 
            position : latlng 
           }); 

       //add info window 

       google.maps.event.addListener(marker, 'mouseover', function() { 
       marker.setIcon(icon2); 
      infowindow.setContent(locations[i]); 
      infowindow.open(map, marker); 
      title: 'Property!' 
       }); 

     google.maps.event.addListener(marker, 'mouseout', function() { 
       marker.setIcon(icon1); 
       infowindow.setContent(locations[i]); 
       infowindow.close(map, marker); 
       title: 'Property!' 
     }); 

      //end of adding info window 

      return marker; 
       } 


       var map = new google.maps.Map(document.getElementById('map'), { 
       zoom : 10, 
       center : new google.maps.LatLng(28.6139, 77.2089), 
       mapTypeId : google.maps.MapTypeId.ROADMAP 
       }); 

      var infowindow = new google.maps.InfoWindow({ 
        size: new google.maps.Size(150,50)}); 

      var geocoder = new google.maps.Geocoder(); 

      for (var i = 0; i < locations.length; i++) { 
       geocodeAddress(i); 

      }//end of for loop 
      </script> 

      </body> 
      </html> 
+0

這個問題看起來很像[你的上一個](http://stackoverflow.com/questions/19581770/markers-are-not-displaying-in-google-map-after-fetching-them-from-database )。其中一個問題應該關閉 – duncan

+0

@教育我刪除了那個問題先生。 – analyticalpicasso

+0

**我的代碼現在正在工作,所以我更新了正確的代碼!** – analyticalpicasso

回答

1

while($add[]=mysql_fetch_row($sql));

從SQL結果獲取儘可能多的行,並將它們放入一個陣列;你會得到一個列數組數組。

PHP中的數組沒有內置方法來自動將它們轉換爲字符串,這是<?=$add?>行上會發生的情況。您需要遍歷它們並單獨打印數組中的每個項目,或者使用諸如implode之類的方法將它們轉換爲字符串。

我懷疑你想要做的是與查詢等

SELECT address FROM markers LIMIT 1

從數據庫中獲取一個地址,這樣做:

$sql=(mysql_query("SELECT address FROM markers LIMIT 1")); 

$addressParts = mysql_fetch_row($sql); 

$add = ($addressParts && count($addressParts)) ? $addressParts[0] : ''; 

這可能會達到什麼樣的你正在尋求實現 - 從數據庫加載一個地址並將其注入到JavaScript中。

+0

感謝您的回覆。解決了地址問題,但它只是從數據庫中獲取第一個地址,並在gmap上顯示單個(第一個)地址。 – analyticalpicasso

+0

從數據庫列中獲取所有行值的方式是什麼,以便我在腳本中傳遞該值以顯示多個標記。 – analyticalpicasso