Jeff Voss
Jeff Voss

Reputation: 3695

MySQL Select inside function to access variables elsewhere

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>           

Upvotes: 0

Views: 265

Answers (2)

Mihai Matei
Mihai Matei

Reputation: 24276

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);

Upvotes: 1

Night2
Night2

Reputation: 1153

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);

Upvotes: 1

Related Questions