Dear Sirs!

I would like to pass the $row[2] from my PHP code below

<?php
// MySQL
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());

// Table
$result = mysql_query("SELECT * FROM  `fillingstations` LIMIT 0 , 30")
or die(mysql_error());  


// Print

while($row = mysql_fetch_array($result)){
echo 'Station name: '. $row[1];
echo 'LatLng: '. $row[2];
echo 'Price: '. $row[3];
}

?>  

to the place of "!!!!Lation!!!!" in my JS code

var beaches = [
  ['Miskolci road / Shell Station', !!!!Lation!!!! 4],
  ['Szabadsag utja / Töltöallomas', 47.559025, 19.281415, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1]
];

Can you help me doing that?

Thanks!

Tibor

var beaches = [
  ['Miskolci road / Shell Station', <?php echo $row[2]; ?> , 4],
Member Avatar for LastMitch

@tibormarias

Since you echo 'LatLng: '. $row[2];

Is this JS code comes before or after or is it on the same page? Never mind I think urtrivedi answer your question.

The PHP code is after. Thank untrivedi I'll try it.

your <script></scipt> tag where your var beaches recides should be inside the while loop of your php and use what urtrivedi said

Do you mean by using PHP echo? echo "<script type="text....

This is my whole JS code:

    function initialize() { 
      var myOptions = {
        zoom: 7,
        center: new google.maps.LatLng(46.985749, 19.293220),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      var map = new google.maps.Map(document.getElementById("map_canvas"),
                                    myOptions);

      setMarkers(map, beaches);
    }
    var beaches = [
      ['Miskolci road / Shell Station', <?php echo $row[2];?>, 4],
      ['Szabadsag utja / Töltöallomas', 47.559025, 19.281415, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];

    function setMarkers(map, locations) {
      var image = new google.maps.MarkerImage('fillingstation.png',
          new google.maps.Size(20, 32),
          new google.maps.Point(0,0),
          new google.maps.Point(0, 32));
      var shadow = new google.maps.MarkerImage('fillingstation.png',
          new google.maps.Size(37, 32),
          new google.maps.Point(0,0),
          new google.maps.Point(0, 32));
      var shape = {
          coord: [1, 1, 1, 20, 18, 20, 18 , 1],
          type: 'poly'
      };
      for (var i = 0; i < locations.length; i++) {
        var beach = locations[i];
        var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            shadow: shadow,
            icon: image,
            shape: shape,
            title: beach[0],
            zIndex: beach[3]
                });
      }

    }
Member Avatar for diafol

You could get all the data from the DB into a php array and then use json_encode to provide the js variable.

$array = array(array('Szabadsag utja / Töltöallomas',47.559025, 19.281415, 5), array('Cronulla Beach',-34.028249, 151.157507,3));

$js = json_encode($array);

...

<script>
var beaches = <?php echo $js;?>;

</script>

Not tested, but something like that.

I 've tried many solutions, and almost all the solutions ended up with no result.. Thank you very much for your idea, but it didn't work neither. I wouldn't have thought this question to be so hard to solve. But I wil play around with json_encode that's not a bad idea.

Member Avatar for diafol
<?php

$array = array(array('Szabadsag utja / Töltöallomas',47.559025, 19.281415, 5), array('Cronulla Beach',-34.028249, 151.157507,3));
$js = json_encode($array);
?>


<script>
    var beaches = <?php echo $js;?>;
    alert(beaches[0][0]);
</script>

This works fine for me. Don't know why it doesn't for you.

Thank you very much, I found a different syntax error on the page, that caused everything... Have a nice day !

Member Avatar for diafol

OK, mark as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.