PHP MySQL Google Chart JSON - Exemple complet
Cette question concerne la génération de Google Charts en utilisant une combinaison de PHP et MySQL. L'utilisation d'Ajax n'est pas couverte dans ce document particulier exemple.
Utilisation
Exigences
Installation
Code Exemples
Exemple de graphique PHP-MySQL-JSON-Google
<?php // Database connection $con = mysql_connect("localhost", "Username", "Password"); mysql_select_db("Database Name", $con); // Query $sth = mysql_query("SELECT * FROM chart"); // Data preparation $table['cols'] = array( array('label' => 'Weekly Task', 'type' => 'string'), array('label' => 'Percentage', 'type' => 'number') ); $rows = array(); while ($r = mysql_fetch_assoc($sth)) { $temp = array(); $temp[] = array('v' => (string)$r['Weekly_task']); $temp[] = array('v' => (int)$r['percentage']); $rows[] = array('c' => $temp); } $table['rows'] = $rows; $jsonTable = json_encode($table); // HTML ?> <html> <head> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script type="text/javascript"> google.load('visualization', '1', {'packages':['corechart']}); google.setOnLoadCallback(drawChart); function drawChart() { var data = new google.visualization.DataTable(<?php echo $jsonTable; ?>); var options = { title: 'My Weekly Plan', is3D: 'true', width: 800, height: 600 }; var chart = new google.visualization.PieChart(document.getElementById('chart_div')); chart.draw(data, options); } </script> </head> <body> <div>
Exemple de graphique PHP-PDO-JSON-MySQL-Google
<?php // Database connection $dbname = 'chart'; $username = 'root'; $password = '123456'; $conn = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Query $result = $conn->query('SELECT * FROM googlechart'); // Data preparation $rows = array(); $table['cols'] = array( array('label' => 'Weekly Task', 'type' => 'string'), array('label' => 'Percentage', 'type' => 'number') ); foreach ($result as $r) { $temp = array(); $temp[] = array('v' => (string)$r['weekly_task']); $temp[] = array('v' => (int)$r['percentage']); $rows[] = array('c' => $temp); } $table['rows'] = $rows; $jsonTable = json_encode($table); // HTML ?> <html> <head> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script type="text/javascript"> google.load('visualization', '1', {'packages':['corechart']}); google.setOnLoadCallback(drawChart); function drawChart() { var data = new google.visualization.DataTable(<?php echo $jsonTable; ?>); var options = { title: 'My Weekly Plan', is3D: 'true', width: 800, height: 600 }; var chart = new google.visualization.PieChart(document.getElementById('chart_div')); chart.draw(data, options); } </script> </head> <body> <div>
PHP-MySQLi-JSON-Google Chart Exemple
<?php // Database connection $DB_NAME = 'chart'; $DB_HOST = 'localhost'; $DB_USER = 'root'; $DB_PASS = '123456'; $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } // Query $result = $mysqli->query('SELECT * FROM googlechart'); // Data preparation $rows = array(); $table['cols'] = array( array('label' => 'Weekly Task', 'type' => 'string'), array('label' => 'Percentage', 'type' => 'number') ); foreach ($result as $r) { $temp = array(); $temp[] = array('v' => (string)$r['weekly_task']); $temp[] = array('v' => (int)$r['percentage']); $rows[] = array('c' => $temp); } $table['rows'] = $rows; $jsonTable = json_encode($table); // HTML ?> <html> <head> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script type="text/javascript"> google.load('visualization', '1', {'packages':['corechart']}); google.setOnLoadCallback(drawChart); function drawChart() { var data = new google.visualization.DataTable(<?php echo $jsonTable; ?>); var options = { title: 'My Weekly Plan', is3D: 'true', width: 800, height: 600 }; var chart = new google.visualization.PieChart(document.getElementById('chart_div')); chart.draw(data, options); } </script> </head> <body> <div>
Dépannage
Si vous rencontrez l'erreur suivante :
syntax error var data = new google.visualization.DataTable(<?php echo $jsonTable; ?>);
Cela indique que votre environnement ne prend pas en charge balises courtes. Pour résoudre ce problème, utilisez plutôt ce code :
<?php echo $jsonTable; ?>
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!