0

i'm trying to generate javascript function dependng on my mysql table and run that generated fuunction. I want to update my google map when new polygon coordinates are added to the database.

loading function is launched when file upload and conversion is done:

function reloadMaps() {
    console.log("reloadMaps");
    //$.get("pages/fields.php", function(data){$('#content').html(data);});
    $.get("addMaps.php", function(data){$('#mapCoordsInclude').html(data);});
    addMaps();
}

addMaps.php file:

<script type="text/javascript">
function addMaps(){
clearMap();


console.log("paleistas update");
<?php



include "dbConfig.php";
// $r = mysql_query("SELECT `fields`.id as id,  `fields`.coordinates as coordinates, `seedings`.culture_id as culture_id, `cultures`.color as color FROM `fields` INNER JOIN seedings ON `seedings`.field_id = `fields`.id INNER JOIN cultures ON `seedings`.culture_id = `cultures`.id where `fields`.farm_id = '4'" );
$r = mysql_query("SELECT id, coordinates from fields where coordinates !='' and farm_id = 4");   


$fields = array();
while($field = mysql_fetch_assoc($r)){
    $fields[$field['id']] = $field;  
$cord = explode(';', $field['coordinates']);
unset($cord[count($cord)-1]);

echo "console.log(\"Naujausias laukas su koordinatemis ".$field['id']."\");\n";
echo "var laukas".$field['id'].";\n";


echo 'var polygonCoords = [';

      foreach ($cord as $key => $c) {
        if($key == count($cord)-1)
          echo "new google.maps.LatLng(".$c.")";
        else
          echo "new google.maps.LatLng(".$c."),";
      }

      echo "];\n";
      echo "laukas".$field['id']." = new google.maps.Polygon({
  paths: polygonCoords, strokeColor: '#000000', strokeOpacity: 0.8, strokeWeight: 2, fillColor: '#000000', fillOpacity: 0.35, id: ".$field['id']." }); laukai.push(laukas".$field['id'].");\n";
    }
    ?>

<?php
echo"\n";
  foreach($fields as $key => $field)
  {
  echo "  laukas". $field['id'].".setMap(map); google.maps.event.addListener(laukas".$field['id'].", 'click', showFieldInfo);\n";
  }
?>
}


function showFieldInfo(event) {
if (document.getElementById('fields') != null) {
      //alert(this.id);
      var page = "pages/showField.php?id=";
      page += this.id;
      $.get(page, function(data){
        $('#content').html(data);
      });
    }
}

</script>

When i fire this script it acctualy runs, adds coordinates to the BD (a seperate function), all addMaps.php runs but the SELECT statement doesn't see new inserted data. If i run it second time by clicking upload form again it works well.

If i delete inserted coordinates from db after first upload and then run upload again - everything also works fine.

Any ideas?

Macke
  • 3
  • 3
  • Instead of generating functions in php, can you not get only required data like your coordinates doing ajax request? – Yalamber Aug 29 '13 at 15:29
  • It might be but i have no idea how to do that.... But i found an answer and did it. It works great. I found the answer in other post: http://stackoverflow.com/questions/9642205/how-to-force-a-script-reload-and-re-execute The main idea is that javascript needs to be REALOADED not just rewrited. – Macke Aug 29 '13 at 15:38

0 Answers0