I need to make a map leaflet that contains some polygons and i want to color my poygons based on data from MYSQL. But when i try this code, my polygons are not show. This is my code on my map.php:
<?php
include 'koneksi.php';
$sql="select sum(skor_bobot) as hasilnya from penilaian where bulan=1 and id_kelurahan=1";
$data=mysql_query($sql);
$js='';
while ($row=mysql_fetch_array($data)) {
$js .='L.geoJson(states, {
style: function(feature) {
if ((feature.properties.party=='.'Republican'.')&&('.$row['hasilnya'].'=='.'10'.')) {
return {color: "#ffff89"};
} else {
return {color: "#ff0000"};
};
}
}).addTo(mymap);';
}
echo $js; ?>
and below are my geojson code on my map.php:
var states = [{
"type": "Feature",
"properties": {"party": "Republican"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-104.05, 48.99],
[-97.22, 48.98],
[-96.58, 45.94],
[-104.03, 45.94],
[-104.05, 48.99]
]]
}
}, {
"type": "Feature",
"properties": {"party": "Democrat"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-109.05, 41.00],
[-102.06, 40.99],
[-102.03, 36.99],
[-109.04, 36.99],
[-109.05, 41.00]
]]
}
}];
Strange that you do not (double) quote Republican
in your JS code?
'(feature.properties.party=='.'"Republican"'.')'
Without those double quotes, once your code is executed as JavaScript, you are passing a variable Republican
(which you have not previously defined, hence your console error), instead of a String "Republican"
to compare with.