How to pass array values from PHP to JavaScript? -
okay. i've been here day, reading , following solutions topic got nothing. nothing works. can show me how it, complete , understandable example. im new javascript , php, how can array values php file javascript. tried doing this: var locations = <?php echo json_encode($location); ?>;
gives me error. , no 1 answered why. code here:
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>google maps multiple markers</title> <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> </head> <body> <div id="map" style="width: 500px; height: 400px;"></div> <script type="text/javascript"> var locations = [ ['municipal hall', 6.414333734068895, 125.61093270778656, 1], ['camarillo household', 6.4345278, 125.58975, 2], ['perez household', 6.4343889, 125.59202777777777, 3], ['usman household', 6.4338056, 125.59191666666666, 4], ['lim household', 6.4333889, 125.59419444444444, 5] ]; var map = new google.maps.map(document.getelementbyid('map'), { zoom: 10, center: new google.maps.latlng(6.40, 125.60), maptypeid: google.maps.maptypeid.roadmap }); var infowindow = new google.maps.infowindow(); var marker, i; (i = 0; < locations.length; i++) { marker = new google.maps.marker({ position: new google.maps.latlng(locations[i][1], locations[i][2]), map: map }); google.maps.event.addlistener(marker, 'click', (function(marker, i) { return function() { infowindow.setcontent(locations[i][0]); infowindow.open(map, marker); } })(marker, i)); } </script> </body> </html>
i want change value of variable locations
value database. got php file:
<?php $servername = "host"; $username = "username"; $password = "password"; $dbname = "dbname"; // create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // check connection if (!$conn) { die("connection failed: " . mysqli_connect_error()); } $sql='select a.household_name, b.latitude, b.longitude household a, location b a.household_id = b.household_id;'; $result = mysqli_query($conn, $sql); if(mysqli_num_rows($result) > 0){ // output data of each row for($i=0;$i<mysqli_num_rows ( $result );$i++){ $row=mysqli_fetch_row($result); $location[]= array($row[0].', '.$row[1].', '.$row[2].','.($i+1)); //echo "household name: " . $row[0]. " - latitude: " . $row[1]. " - longitude " . $row[2]. " " .($i+1)."<br>"; } }else{echo "0 results";} ?>
it works fine. outputs data database. want know how can convert value database value can use in place of locations
variable markers appear on map? var locations = <?php echo json_encode($location); ?>;
guy give me error, followed every instruction can, still it's error. can u modify code works or can u point me out in working/functional code there knowledge. please, me. i'm in deep trouble here.
try change location[]
variable :
$location[]= array($row[0].', '.$row[1].', '.$row[2].','.($i+1));
to
$location[]= array($row[0],$row[1],$row[2],($i+1));
i have added full code below , working :
<?php $servername = "host"; $username = "username"; $password = "password"; $dbname = "dbname"; // create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // check connection if (!$conn) { die("connection failed: " . mysqli_connect_error()); } $sql='select a.household_name, b.latitude, b.longitude household a, location b a.household_id = b.household_id;'; $result = mysqli_query($conn, $sql); if(mysqli_num_rows($result) > 0){ // output data of each row for($i=0;$i<mysqli_num_rows ( $result );$i++){ $row=mysqli_fetch_row($result); $location[]= array($row[0],$row[1],$row[2],($i+1)); //echo "household name: " . $row[0]. " - latitude: " . $row[1]. " - longitude " . $row[2]. " " .($i+1)."<br>"; } }else{echo "0 results"; } ?> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>google maps multiple markers</title> <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> </head> <body> <div id="map" style="width: 500px; height: 400px;"></div> <script type="text/javascript"> /*var locations = [ ['municipal hall', 6.414333734068895, 125.61093270778656, 1], ['camarillo household', 6.4345278, 125.58975, 2], ['perez household', 6.4343889, 125.59202777777777, 3], ['usman household', 6.4338056, 125.59191666666666, 4], ['lim household', 6.4333889, 125.59419444444444, 5] ];*/ var locations = <?php echo json_encode($location); ?>; var map = new google.maps.map(document.getelementbyid('map'), { zoom: 10, center: new google.maps.latlng(6.40, 125.60), maptypeid: google.maps.maptypeid.roadmap }); var infowindow = new google.maps.infowindow(); var marker, i; (i = 0; < locations.length; i++) { marker = new google.maps.marker({ position: new google.maps.latlng(locations[i][1], locations[i][2]), map: map }); google.maps.event.addlistener(marker, 'click', (function(marker, i) { return function() { infowindow.setcontent(locations[i][0]); infowindow.open(map, marker); } })(marker, i)); } </script> </body> </html>
Comments
Post a Comment