php - 如何使用 PHP 变量查询 MySQL 并通过 Google Charts 显示结果?

标签 php mysql variables google-visualization

我刚刚开始使用 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/

相关文章:

php - Bootstrap 列在 Wordpress 上发生冲突

javascript - 使用jquery生成动态href然后读取<a>的类

php - 用 WooCommerce 3 中选择的变化价格替换可变价格范围

MySql 查询比它应该花费的时间长得多

c++ - 如何设置不定数量的变量,由另一个变量指定

php - 登录和密码更改 PHP 和 MYsql

Mysql 工作台创建函数

mysql - 以绝对位置排序结果

php - 在 PHP echo 中嵌入 javascript

javascript - 将javascript变量传递给html文本框