我刚刚开始使用 PHP 和 MySQL 的 Google Charts API,希望得到一些帮助。我希望 Google Charts 使用的 MySQL 查询结果基于变量。基本上我有这个并且它按预期工作。
显示图表.php
<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 jsonData = $.ajax({
url: "test3.php",
dataType: "json",
async: false
}).responseText;
var data = new google.visualization.DataTable(jsonData);
var options = {
fontName: 'Trebuchet MS',
colors: ['#22671F'],
title: 'Handicap History',
hAxis: {title: 'Revision Date', titleTextStyle: {color: 'red'}}
};
var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
test3.php
$result = $connect->query("SELECT rev_date, ndx FROM revisions WHERE player_id=7");
$table = array();
$table['cols'] = array(
array('label' => 'Rev', 'type' => 'string'),
array('label' => 'Index', 'type' => 'number')
);
$rows = array();
while ($nt = $result->fetch_assoc())
{
$temp = array();
$temp[] = array('v' => $nt['rev_date']);
$temp[] = array('v' => $nt['ndx']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
echo $jsonTable;
我希望能够在 test3.php 中执行类似的操作
$result = $connect->query("SELECT rev_date, ndx FROM revisions WHERE player_id='$playerID'");
$table = array();
$table['cols'] = array(
array('label' => 'Rev', 'type' => 'string'),
array('label' => 'Index', 'type' => 'number')
);
$rows = array();
while ($nt = $result->fetch_assoc())
{
$temp = array();
$temp[] = array('v' => $nt['rev_date']);
$temp[] = array('v' => $nt['ndx']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
echo $jsonTable;
如何将 $playerID 变量的值传递到 test3.php 页面?当在 ajax 中调用 url: "test3.php"
片段时,我可以传递一个变量并在 test3.php 上使用 $_GET 吗?或者还有其他方法可以做到这一点吗?
提前非常感谢您。
最佳答案
在$.ajax()
方法中,您可以使用options
中的data
属性。
示例:
var jsonData = $.ajax({
url: "test3.php",
dataType: "json",
async: false,
data: { 'playerID' : 123 }
}).responseText;
根据我对您有关 SYNC 命令的问题的评论...您应该使用可用于触发下一个函数的回调
方法。
关于php - 如何使用 PHP 变量查询 MySQL 并通过 Google Charts 显示结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17352821/