基本上我創建一個動態的谷歌地圖,從數據庫中提取信息,連接是好的,查詢是好的 - 但我無法訪問我的變量($行)從我的功能。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>
'$行= $ sql-> fetch_array();' 這裏'$ row'是該函數的一個局部變量getCurrentMapData($ select)',不是嗎?嘗試從函數返回'$ row'。 – shibly