我正在创建我的第一个仪表板站点,以便向各种用户动态显示MySQL数据。对于某些上下文,我正在使用Webmatrix PHP入门站点,并且无法确定如何将数据查询为.json格式以供Google Charts API读取。我已成功硬编码数组以使用.ajax传递到Google Chart,但是从SQL查询构建数组时遇到问题。
我整个星期都在寻找一个解决方案,但还没找到。
我当前的代码在下面并返回“表没有列”错误。
-Query并解析为.json(未解决)
<?php
// -----> Query MySQL and parse into JSON below. <------
// write your SQL query here (you may use parameters from $_GET or $_POST if you need them)
require_once ("Includes/session.php");
require_once ("Includes/simplecms-config.php");
require_once ("Includes/connectDB.php");
include("Includes/header.php");
$recId = null;
$projid = null;
$hours = null;
$recId = $_GET['id'];
$projid = $_GET['projid'];
$hours = $_GET['hours'];
$query = "SELECT projid, hours FROM hours WHERE id = ?";
$statement = $databaseConnection->prepare($query);
$statement->bind_param('d', $recId);
$statement->execute();
$statement->store_result();
$rows = array();
$table = array();
$table['cols'] = array(
array('label' => 'projid', 'type' => 'string'),
array('label' => 'hours', 'type' => 'number')
);
/* Extract the information from $result */
foreach($result as $r) {
$temp = array();
// The following line will be used to slice the Pie chart
$temp[] = array('v' => (string) $r['projid']);
// Values of the each slice
$temp[] = array('v' => (int) $r['hours']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
// convert data into JSON format
$jsonTable = json_encode($table);
//echo $jsonTable;
&GT;
-Load .ajax并绘制图表(已解决)
<?php
include("Includes/header.php");
include("Includes/charts/piecharthours.php");
include("Includes/charts/linechart.php");
?>
<html>
<head>
<!--Load the AJAX API-->
<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">
// Load the Visualization API and the piechart package.
google.load('visualization', '1', { 'packages': ['corechart'] });
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "getDatahours.php",
dataType: "json",
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, { width: 400, height: 240 });
setInterval(drawChart, 500);
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div" style="width: 500px; height: 500px;"></div>
</body>
</html>
- 控制台输出
Uncaught Error: Invalid JSON string: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Dashboard</title>
<link href="/Styles/Site.css" rel="stylesheet" type="text/css" />
<link href="/Styles/Sitemenu.css" rel="stylesheet" type="text/css" />
<link href="/Styles/site2.css" rel="stylesheet" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<body>
<div class="outer-wrapper">
<header>
<div class="content-wrapper">
<div class="float-left">
<p class="site-title">
<a href="/index.php" class="uclogo">UC Logo</a>
<a href="/index.php">UC</a></p>
</div>
<div class="float-right">
<section id="login">
<ul id="login">
<li><a href="/logoff.php">Sign out</a></li>
<li><a href="/addpage.php">Add</a></li>
<li><a href="/selectpagetoedit.php">Edit</a></li>
<li><a href="/deletepage.php">Delete</a></li>
</ul>
<div class="welcomeMessage">Welcome, <strong>ID</strong></div>
</section>
</div>
<div class="clear-fix"></div>
</div>
<section class="navigation" data-role="navbar">
<nav>
<ul id="menu">
<li><a href="/index.php">Dashboard</a></li>
<li><a href="/projects.php">Projects</a>
<ul>
<li><a href="/page.php?pageid=1">P4</a></li>
<li><a href="/page.php?pageid=2">P1</a></li>
<li><a href="/page.php?pageid=3">P2</a></li>
<li><a href="/page.php?pageid=4">P3</a></li>
</ul>
<li><a href="/resources.php">Resources</a></li>
<li><a href="/reporting.php">Reporting</a></li>
<li><a href="/help.php">Help</a></li>
</ul>
</nav>
</section>
</header> format+en,default,corechart.I.js:60
Uncaught Error: Container is not defined example.php:6
Uncaught Error: Invalid JSON string: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Dashboard</title>
<link href="/Styles/Site.css" rel="stylesheet" type="text/css" />
<link href="/Styles/Sitemenu.css" rel="stylesheet" type="text/css" />
<link href="/Styles/site2.css" rel="stylesheet" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<body>
<div class="outer-wrapper">
<header>
<div class="content-wrapper">
<div class="float-left">
<p class="site-title">
<a href="/index.php" class="uclogo">UC Logo</a>
<a href="/index.php">UC</a></p>
</div>
<div class="float-right">
<section id="login">
<ul id="login">
<li><a href="/logoff.php">Sign out</a></li>
<li><a href="/addpage.php">Add</a></li>
<li><a href="/selectpagetoedit.php">Edit</a></li>
<li><a href="/deletepage.php">Delete</a></li>
</ul>
<div class="welcomeMessage">Welcome, <strong>ID</strong></div>
</section>
</div>
<div class="clear-fix"></div>
</div>
<section class="navigation" data-role="navbar">
<nav>
<ul id="menu">
<li><a href="/index.php">Dashboard</a></li>
<li><a href="/projects.php">Projects</a>
<ul>
<li><a href="/page.php?pageid=1">P4</a></li>
<li><a href="/page.php?pageid=2">P1</a></li>
<li><a href="/page.php?pageid=3">P2</a></li>
<li><a href="/page.php?pageid=4">P3</a></li>
</ul>
<li><a href="/resources.php">Resources</a></li>
<li><a href="/reporting.php">Reporting</a></li>
<li><a href="/help.php">Help</a></li>
</ul>
</nav>
</section>
</header>