Click here to Skip to main content
15,883,990 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.

JavaScript
$name
$user_date
...

Whats wrong with my code? Am I missing something? TYFH and IA
Posted
Updated 26-Feb-21 22:45pm

1 solution

<?php
$result = mysqli_query($con1, "SELECT * FROM bank ORDER BY id DESC;");
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
$a1 = $row['a1'];
$a2 = $row['a2'];
$a3 = $row['a3'];
$a4 = $row['a4'];
}}
if ($a4 == "" or $a3 == ""){
$taschart_1 = "-86.721524, 96.332683" ; 
}else{
$chart = $a4 . "," . $a3 ;
}
?>
var roadeye = L.icon({iconUrl: 'assets/img/ta.png',iconSize: [30, 30],popupAnchor: [0,-15]});
var customPopup = "<br>data : <?php echo"$a1"?><br/><br>time : <?php echo"$a2"?><br/>";
var customOptions =
{'maxWidth': '130','className' : 'custom'}   
L.marker([<?php echo"$chart"?>], {icon: roadeye}).bindPopup(customPopup,customOptions).addTo(map);
--------------------------------------------------------
hi, please help me.
database data to leaflet.
This is my code. But it only brings one point.
 
Share this answer
 

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