2

I have a PHP array of country names -

<?php 
$countries_array = array("Russia", "Australia", "Chile");
?>

I need to show these countries on map via the Google Geochart map in Javascript and using this code -

function drawRegionsMap() {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Country');

<?php 
    foreach ($countries_array as $cty) {
        echo "data.addRow(['Country', " . $cty . "]);";
    }
?>

    var options = {backgroundColor: '#E7F2F4'};

    var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));

    chart.draw(data, options);
    }

I guess I'm passing the countries array in a wrong way, because the map doesn't show these countries. How can I correct it ?

Gissipi_453
  • 1,250
  • 1
  • 25
  • 61

1 Answers1

1

Note PHP serves $countries_array so you need to be within PHP script tags to output the array. It is probably easier to add each data using addRow(), it makes the syntax simpler.

function drawRegionsMap() {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Country');

<?php 
    foreach ($countries_array as $cty) {
        print "data.addRow(['" . $cty . "']);" . PHP_EOL;
    }
?>

  var options = {backgroundColor: '#cccccc'};
  var chart = new   google.visualization.GeoChart(document.getElementById('regions_div'));

  chart.draw(data, options);
}
Gissipi_453
  • 1,250
  • 1
  • 25
  • 61
suspectus
  • 16,548
  • 8
  • 49
  • 57
  • still not working. the map doesn't appear now. updated my question with my code @suspectus – Gissipi_453 Sep 14 '19 at 09:40
  • 1
    thanks. your answer works with just s small modification. by removing 'Country', from - print "data.addRow(['Country', '" . $cty . "']);" . PHP_EOL; – Gissipi_453 Sep 14 '19 at 10:06