1.11M Members

Pass PHP string to JS

 
0
 

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

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

@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.

 
0
 

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

 
0
 

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

 
0
 

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

    }
 
0
 

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.

 
0
 

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.

 
1
 
<?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.

 
0
 

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

 
0
 

OK, mark as solved.

Question Answered as of 1 Year Ago by diafol, LastMitch, code739 and 2 others
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article