0

Basically I am creating a dynamic google map that pulls information from the database, the connection is fine, the query is fine - but I'm unable to access my variables ($row) from my function.

Should I be parsing all the javascript from inside the php function itself? or can I have it static on my page and access the variables simply by calling the function (getCurrentMapData) directly above the javascript?

function getCurrentMapData( $select ) 
{
    global $mysqli;

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

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

    $row = $sql->fetch_array(); 

}

And here's our outset:

<?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>           
1
  • $row = $sql->fetch_array(); Here $row is a local variable for that function getCurrentMapData( $select ) , isn't it? Try returning that $row from function. Commented Sep 18, 2012 at 4:27

2 Answers 2

1

You should return the fetched array

function getCurrentMapData( $select )  
{ 
    global $mysqli; 

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

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

    return $sql->fetch_array();  

} 

Then you should assign the result of this function to the $row variable

$row = getCurrentMapData($select);

Sign up to request clarification or add additional context in comments.

Comments

1

In your php function add this at the end:

return $row;

This will output your $row out of function, then in your code, change

getCurrentMapData( $select );

to this:

$row = getCurrentMapData($select);

2 Comments

AHH I KNEW it was something just like that - thank you kind sir
@Jeff Or you could simply return getCurrentMapData($select);, since $row isn't used within getCurrentMapData()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.