1,105,226 Community Members

Pass PHP string to JS

Member Avatar
tibormarias
Light Poster
43 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
urtrivedi
Posting Virtuoso
1,897 posts since Dec 2008
Reputation Points: 249 [?]
Q&As Helped to Solve: 408 [?]
Skill Endorsements: 27 [?]
 
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.

Member Avatar
tibormarias
Light Poster
43 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
code739
Posting Whiz in Training
213 posts since May 2012
Reputation Points: 17 [?]
Q&As Helped to Solve: 30 [?]
Skill Endorsements: 6 [?]
 
0
 

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

Member Avatar
tibormarias
Light Poster
43 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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]
                });
      }

    }
Member Avatar
diafol
Where are my eyes?
12,941 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
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.

Member Avatar
tibormarias
Light Poster
43 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
diafol
Where are my eyes?
12,941 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
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.

Member Avatar
tibor.marias
Newbie Poster
13 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
diafol
Where are my eyes?
12,941 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
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