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 ?

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

still not working. the map doesn't appear now. updated my question with my code @suspectus
thanks. your answer works with just s small modification. by removing 'Country', from - print "data.addRow(['Country', '" . $cty . "']);" . PHP_EOL;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.