Php Google Geo Chart Example
Teams applying “Php Google Geo Chart Example” in a production project can use this workflow reference to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the PHP project and test it against the project’s actual database and runtime.
Hi Dev,
In this blog, I would like to share with you how to implement google geo chart in php. We will show create dynamic google geo chart in php and mysql. Make a simple chart by google chart api with php mysql. in this tutorial i going to learn you how to create google geo chart using google api in php with mysql.
In this example i am create populer framework chart . and I will show total popularity for framework using google geo chart in php with mysql.
Here I will give you full example for create dynamic google geo chart using google api in php, so let's follow bellow step by step.
Step 1 : Create Table
CREATE TABLE `framework` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` varchar(70) NOT NULL,
`number` int(11) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Step 2 : Configuration
Create a config.php file for the database configuration.
config.php
<?php
$host = "localhost"; /* Host name */
$user = "root2"; /* User */
$password = "root"; /* Password */
$dbname = "post_data"; /* Database name */
$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$chartQuery = "SELECT * FROM pte_chart";
$chartQueryRecords = mysqli_query($con,$chartQuery);
?>
Step 3 : HTML & PHP
In this step,you can use google geo chart api and create google geo chart in this file. Display records from employee table and generate google geo chart with the test table.
index.php
<?php
include "config.php";
?>
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<body style="text-align: center;">
<h2 class="text-center">Generate Geo Chart in PHP</h2>
<div id="regions_div" style="width: 900px; height: 500px;"></div>
<div class="text-center">
<h2>nicesnippets.com</h2>
</div>
<script type="text/javascript">
google.charts.load('current', {
'packages':['geochart'],
'mapsApiKey': 'AIzaSyD-9tSrke72PouQMnMX-a7eZSW0jkFMBWY'
});
google.charts.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {
var data = google.visualization.arrayToDataTable([
['Country', 'Popularity'],
<?php
while($row = mysqli_fetch_assoc($chartQueryRecords)){
echo "['".$row['name']."', ".$row['number']."],";
}
?>
]);
var options = {
title: 'Popularity of Country',
};
var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
chart.draw(data, options);
}
</script>
</head>
</body>
</html>
Now we are ready to run our example so run bellow command for quick run:
php -S localhost:8000
Now you can open bellow URL on your browser:
http://localhost:8000/index.php
It will help you...
- How To Use Php Conditional Statement Example
- How to Check if String Contains Regex in PHP?
- PHP Array_column() Function Example
- PHP Generate List of Random Numbers Between 0 and 100 Example
- How to User Login with Facebook in PHP?
- How to Explode Every Character into Array Using PHP Example?
- PHP Form Validation Tutorial
- PHP Convert XML to JSON Example