PHP Loop only returns last variable
-
I have successfully passed my php variables to my javascript code, but it’s only grabbing the last variable. I’m assuming it’s my loop that is incorrect but I’ve been struggling with this for over a week now so any help would be greatly appreciated. What I’m trying to accomplish is that when the name of the custom field is clicked, that specific location will drop on the map and replace the old marker with the new. I’m still a newbie with all of this.
Here is my PHP
function add_scripts() { wp_register_script( 'marker', get_stylesheet_directory_uri() . '/js/marker.js', array('jquery') ); $posts = get_posts('locations'); $longs = array(); $lats = array(); foreach($posts as $post){ array_push($lats, rwmb_meta('latitude') ); array_push($longs, rwmb_meta('longitude') ); } $passedValues = array( 'latitude' => $lats, 'longitude' => $longs, ); wp_localize_script( 'marker', 'passed_object', $passedValues ); wp_enqueue_script('marker', get_template_directory_uri() . '/js/marker.js', array('jquery') ); } add_action( 'wp_enqueue_scripts', 'add_scripts' );and here is my Java
function dropMarker(elem) { //function that will add markers on button click var latitude = passed_object.latitude[$( elem ).attr('id')]; var longitude = passed_object.longitude[$( elem ).attr('id')]; var marker = new google.maps.Marker({ position: new google.maps.LatLng(latitude, longitude), map: map, animation: google.maps.Animation.DROP, }); if (oldMarker != undefined) { oldMarker.setMap(null); } oldMarker = marker; map.setCenter(marker.getPosition()); }The page I need help with: [log in to see the link]
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
The topic ‘PHP Loop only returns last variable’ is closed to new replies.