PHP MySQL Google Chart JSON:綜合範例
本指南提供了一個綜合範例,介紹如何使用來自MySQLSQL 表。我們將示範 PHP、MySQL 和 Google Chart API 的集成,以建立視覺化表示資料。
要求:
安裝:
PHP-MySQL-JSON-Google Chart範例:
<?php // Connect to the MySQL database $con = mysql_connect("localhost", "Username", "Password") or die("Failed to connect with database!!!!"); mysql_select_db("Database Name", $con); // Query the database for weekly tasks and percentages $sth = mysql_query("SELECT * FROM chart"); // Prepare the data for the Google Chart $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); // Load the Google Chart API and create a pie chart ?> <html> <head> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript"> google.load('visualization', '1', {'packages':['corechart']}); google.setOnLoadCallback(drawChart); function drawChart() { var data = new google.visualization.DataTable(<?=$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 echo $jsonTable; ?>
以上是如何使用 PHP 和 JSON 從 MySQL 資料建立 Google 圖表?的詳細內容。更多資訊請關注PHP中文網其他相關文章!