0

基本上我正在创建一个从数据库中提取信息的动态谷歌地图,连接很好,查询很好 - 但我无法从我的函数访问我的变量($row)。

我应该从 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>           
4

2 回答 2

1

在您的 php 函数中在最后添加:

return $row;

这将输出你$row的功能,然后在你的代码中,改变

getCurrentMapData( $select );

对此:

$row = getCurrentMapData($select);
于 2012-09-18T04:27:26.433 回答
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);

于 2012-09-18T04:29:26.987 回答