Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, you are on the right track, but you just need to understand the separate concepts of server-side and client-side languages and how they can interact. PHP doesn't "pass" variables to JavaScript, but what it does do is generate whatever HTML document you want.</p> <p>That HTML document then can contain JavaScript, which will execute as the page is <em>rendered</em> by the browser. So, think of your PHP as <em>making</em> the JavaScript code:</p> <p><strong>Example of PHP outputting JavaScript code in HTML page:</strong></p> <pre><code>&lt;script type="text/javascript"&gt; var testval = "&lt;?php echo "Hello, " . (5 + 3) . "!" ?&gt;"; // "Hello, 8! &lt;/script&gt; </code></pre> <p>Now, I looked up the tutorial, and actually the code in your question is not the right code &mdash; instead it is the code to create the map, and the lat/long parameters in your example are for the center, not a marker.</p> <p>So, in your PHP page, you want to do the following:</p> <ol> <li>Somewhere, you need to create the map: <code>var map = new google.maps.Map...</code> (as shown in the tutorial)</li> <li>Next, get the <code>$map</code> array with array items containing the <code>'lng'</code> and <code>'lat'</code> keys. <em>(Note: you should always wrap array key names with quotes)</em></li> <li><strong>Inside an opened script tag</strong> use <code>&lt;?php</code> to create a PHP code block, and create your <code>foreach</code> loop. For each item, create the JavaScript code needed to create the marker.</li> </ol> <p><strong>Example of foreach loop:</strong></p> <pre><code>&lt;script type="text/javascript"&gt; &lt;?php foreach($map as $row) { $lng = $row['lng']; $lat = $row['lat']; ?&gt; // Creating a marker and positioning it on the map new google.maps.Marker({ position: new google.maps.LatLng(&lt;?php echo $lat ?&gt;, &lt;?php echo $lng; ?&gt;), map: map }); &lt;?php } ?&gt; &lt;/script&gt; </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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