Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL data in javascript using PHP's foreach loop (Googlel Maps API)
    primarykey
    data
    text
    <p>I'm trying to use the Google Maps V3 API to create markers on a google map. I have the coordinates of the markers in mySQL database, and is currently in a PHP array in my .php file. Now how do I use a foreach() loop (or another suitable method) to loop through the elements in my PHP array and create a new google map marker with each iteration of the loop?</p> <p>PS: My PHP is decent, but not my javscript knowledge. The tutorial I'm following now on creating the markers is at <a href="http://www.svennerberg.com/2009/07/google-maps-api-3-markers/" rel="nofollow">http://www.svennerberg.com/2009/07/google-maps-api-3-markers/</a></p> <p><strong>Code</strong></p> <p>I'm using Codeigniter framework, so the controller+model file already retrieved the necessary data(name, lng, lat...) into an array $map. I can loop the array using the usual method:</p> <pre><code>foreach($map as $row) { $lng = $row[lng] // this is not necessary, its just to show what is in the array $lat = $row[lat] // now how do I use this loop to create new marker code in javascript? } </code></pre> <p>The JS code for creating a google map marker which has to be created once per foreach loop iteration is like:</p> <pre><code>var map = new google.maps.Map(document.getElementById('map'), { zoom: 7, center: new google.maps.LatLng($lng, $lat), // how do i pass the PHP variables into the JS code? mapTypeId: google.maps.MapTypeId.ROADMAP }); </code></pre> <p>So my question is how do i pass the PHP variables in the PHP array to create the JS code above, once per each foreach() loop iteration?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload