2012-09-18 94 views
0

基本上我創建一個動態的谷歌地圖,從數據庫中提取信息,連接是好的,查詢是好的 - 但我無法訪問我的變量($行)從我的功能。MySQL選擇裏面的函數來訪問其他地方的變化

我應該從php函數本身解析所有的javascript嗎?或者我可以在我的頁面上使用靜態方法,並通過直接調用javascript之上的函數(getCurrentMapData)來訪問變量?

function getCurrentMapData($select) 
{ 
    global $mysqli; 

    $sql = $mysqli->query($select); 

    if(!$sql) { 
     printf("%s\n", $mysqli->error); 
     exit(); 
    } 

    $row = $sql->fetch_array(); 

} 

這裏是我們一開始就:

<?php 

    $select = "SELECT `id`, `title`, `address`, `lat`, `lng`, `date` FROM `globalmap` ORDER BY `id` desc "; 

    getCurrentMapData($select); 

?> 

       <h3> 
        I'm Currently Playing @ 
       <?php echo $row['title'] ?> 
       </h3> 
       <div id="map"> 
        <div id="ps_map_1" class="mapCon"> 
        </div> 
       </div> 
       <script src="http://maps.google.com/maps/api/js?sensor=false"></script> <script> 
        // <![CDATA[ 


         var psGlobals = { 
          address: "<?php echo $row['address'] ?>", 
          lat: <?php echo $row['lat'] ?>, 
          lon: <?php echo $row['lng'] ?>, 
          zoomlevel: 13 
         }; 

         function initialize() 
         { 
          psGlobals.latlng = new google.maps.LatLng(psGlobals.lat, psGlobals.lon); 
          buildMap(); 
          psGlobals.dirRenderer = new google.maps.DirectionsRenderer(); 
          psGlobals.dirService = new google.maps.DirectionsService(); 
         } 

         function buildMap() 
         { 
          var myOptions, marker; 
          myOptions = { 
            navigationControl: true, 
            navigationControlOptions: { 
             style: google.maps.NavigationControlStyle.ZOOM_PAN 
            }, 

            mapTypeControl: true, 
            scaleControl: false, 
            zoom: psGlobals.zoomlevel, 
            center: psGlobals.latlng, 
            mapTypeId: google.maps.MapTypeId.HYBRID 
          }; 
          psGlobals.map = new google.maps.Map(document.getElementById("ps_map_1"), myOptions); 
          psGlobals.marker = new google.maps.Marker({ 
           position: psGlobals.latlng, 
           map: psGlobals.map, 
           title: "<?php echo $row['title'] ?>" 
          }); 

         } 

         $(document).ready(function(){ 
          initialize() 
         }); 

        // ]]> 

        </script>   
+0

'$行= $ sql-> fetch_array();' 這裏'$ row'是該函數的一個局部變量getCurrentMapData($ select)',不是嗎?嘗試從函數返回'$ row'。 – shibly

回答

1

您應該返回取陣列

function getCurrentMapData($select) 
{ 
    global $mysqli; 

    $sql = $mysqli->query($select); 

    if(!$sql) { 
     printf("%s\n", $mysqli->error); 
     exit(); 
    }  

    return $sql->fetch_array(); 

} 

那麼你應該將這個函數的結果分配給$row變量

$row = getCurrentMapData($select);

1

在你的PHP函數在最後補充一點:

return $row; 

這將輸出你的$row出來的功能,然後在你的代碼,變更

getCurrentMapData($select); 

這樣:

$row = getCurrentMapData($select); 
+0

AHH我知道這是類似的東西 - 謝謝你先生 – iamwhitebox

+0

@Jeff或者你可以簡單地'返回getCurrentMapData($ select);',因爲'getCurrentMapData() – yannis