Click here to Skip to main content
15,883,558 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Day too all! I just wanna ask a little question. Ahmm how do you show data from database to leaflet pop up?

Here is my code in php select command


JavaScript
<?php 
$db = new PDO('mysql:host=localhost;dbname=poi', 'root', ''); 
$sql = "SELECT name,user_date,user_time,address,lat,lng,icon_name FROM tblmarker"; 
 
$rs = $db->query($sql); 
if (!$rs) { 
    echo "An SQL error occured.\n"; 
    exit; 
} 
 
$rows = array(); 
while($r = $rs->fetch(PDO::FETCH_ASSOC)) { 
    $rows[] = $r; 
    $name[] = $r['name'];
    $user_date[] = $r['user_date'];
    $user_time[] = $r['user_time'];
    $address[] = $r['address'];
    $icon_name[] = $r['icon_name'];
} 
print json_encode($rows); 
$db = NULL; 
?> 

and here is my code in showing the marker in the map from database


JavaScript
function getInfo() { 
        $.getJSON("get_info.php", function (data) { 
          for (var i = 0; i < data.length; i++) { 
            var location = new L.LatLng(data[i].lat, data[i].lng); 
            var marker = new L.Marker(location,{icon:Icon1}); 
            var ll = marker.getLatLng();
 
          marker.bindPopup("$name<br>$user_date<br>$user_time<br>$address<br>$icon_name").addTo(map); 
 
             } 
        }); 
      } 

I thought it works because when i refresh it the marker from my database shows in the map but when i click the marker for the pop up the pop up shows only is like this.


SQL
$name
$user_date

...
Whats wrong with my code? Am I missing something? TYFH and IA
Posted
Updated 30-Nov-14 15:17pm
v3
Comments
Prava-MFS 1-Dec-14 0:44am    
You used PHP variable inside javascript. That's why in the pop-up it shows $name and $user_date. I will add the solution below, so please stay tuned for my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900