简体   繁体   English

使用PHP和MySQL构建Google Chart

[英]Building a Google Chart with PHP and MySQL

I am pretty new at PHP and MySQL queries. 我是PHP和MySQL查询的新手。 I am trying to build a Google chart from a MySQL database but after searching on Google for countless hours I could not find what I need. 我正在尝试从MySQL数据库构建一个谷歌图表,但在谷歌搜索无数个小时后,我找不到我需要的东西。 However I found an example that could be useful but I still can't make it the way I want. 但是我发现了一个可能有用的例子,但我仍然不能按照我想要的方式进行。 Here's an example of my table. 这是我的表的一个例子。

    Apple   | Orange  | Strawberry
    --------------------------
    Like    | Like    | Like
    Dislike | Like    | Like
    Dislike | Dislike | Like
    Like    | Dislike | Dislike
    Like    | Like    | Like

I want to count how many Like and Dislike for Apple , Orange and Strawberry . 我想算一下AppleOrangeStrawberry多少喜欢和Dislike In the chart I want it to display how many people like and dislike these 3 fruits. 在图表中我希望它显示有多少人喜欢和不喜欢这三种水果。

Here's the code I've been looking at and I've yet figured out how to attack it. 这是我一直在看的代码,我还想出了如何攻击它。

    $query = mysql_query('SELECT * FROM data');

    $table = array();
    $table['cols'] = array(
        array('label' => 'cas', 'type' => 'string'),
        array('label' => 'data', 'type' => 'number')
    );

    $rows = array();
    while($r = mysql_fetch_assoc($query)) {
        $temp = array();
        $temp[] = array('v' => $r['cas']);
        $temp[] = array('v' => (int) $r['data']);

    $rows[] = array('c' => $temp);
    }

    $table['rows'] = $rows;

    $jsonTable = json_encode($table);

    echo $jsonTable;

Any example would help! 任何例子都会有帮助! Thank you. 谢谢。

Try this as your PHP: 试试这个作为你的PHP:

$query = mysql_query("
    SELECT
        'Like' as 'preference',
        SUM(IF(Apple = 'Like', 1, 0)) as Apple,
        SUM(IF(Orange = 'Like', 1, 0)) as Orange,
        SUM(IF(Strawberry = 'Like', 1, 0)) as Strawberry
    FROM data
    UNION
    SELECT
        'Dislike' as 'preference',
        SUM(IF(Apple = 'Dislike', 1, 0)) as Apple,
        SUM(IF(Orange = 'Dislike', 1, 0)) as Orange,
        SUM(IF(Strawberry = 'Dislike', 1, 0)) as Strawberry
    FROM data
");

$table = array();
$table['cols'] = array(
    array('label' => 'preference', 'type' => 'string'),
    array('label' => 'Apple', 'type' => 'number'),
    array('label' => 'Orange', 'type' => 'number'),
    array('label' => 'Strawberry', 'type' => 'number')
);

$rows = array();
while($r = mysql_fetch_assoc($query)) {
    $temp = array();
    $temp[] = array('v' => $r['preference']);
    $temp[] = array('v' => (int) $r['Apple']);
    $temp[] = array('v' => (int) $r['Orange']);
    $temp[] = array('v' => (int) $r['Strawberry']);

    $rows[] = array('c' => $temp);
}

$table['rows'] = $rows;

$jsonTable = json_encode($table);

echo $jsonTable;

The SQL should return two rows of data, one a sum of likes and the other a sum of dislikes, which then gets parsed into the Google Visualization API DataTable format and echo'd as a JSON string. SQL应返回两行数据,一行是喜欢的总和,另一行是不喜欢的数据,然后将其解析为Google Visualization API DataTable格式并作为JSON字符串进行回显。 This is good for use as an AJAX data source for the chart, but with a minor modification, it would be suitable for directly outputting the data into the javascript for drawing a chart. 这适合用作图表的AJAX数据源,但只需稍作修改,就可以直接将数据输出到javascript中绘制图表。

"Here's the code I've been looking at and I've yet figured out how to attack it." “这是我一直在寻找的代码,我还想出了如何攻击它。”

Well here's how to debug it, say these are the steps you are taking. 那么这里是如何调试它,说这些是您正在采取的步骤。

  1. Get data from db 从db获取数据
  2. Create an array 创建一个数组
  3. Json encode the array Json对数组进行编码
  4. Send the Json to the chart 将Json发送到图表

Try and hard code an array at step 2. above, removing step 1 from the equation for a moment. 在上面的步骤2.尝试并硬编码数组,从等式中移除步骤1

Now, going forward from that step, does the rest of the code as it should? 现在,从该步骤开始,其余的代码是否应该如此? Can you see the chart that expect using the hard-coded values? 你能看到使用硬编码值的图表吗?

If yes, well now work backwards, var_dump() ing data till it matches the hard-coded values you had previously. 如果是,那么现在可以向后工作, var_dump()数据,直到它与您之前的硬编码值匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM