dow72046 2013-05-13 12:48
浏览 45
已采纳

如何将PHP数组嵌入到谷歌图表脚本中?

first of all sorry this is so amatuer, I'm still very much a beginner.

I'm practising interacting with databases in PHP and displaying it nicely using Google Visualization. However my chart won't display and I think it's because of how I'm passing the data into the chart, since I've used Google Charts before and the only thing I'm doing differently is using $row to put the info into the chart.

Or am I going about this the wrong way and I should be putting $row into a new array and then passing that into the chart?

Many thanks!

Here is my code:

<?php
//this script retrieves all data from the fruit table and displays it in a google chart

$page_title = "View the fruit table";

require_once ('connect.php'); //connects to mysql db

//make the query
$query = "SELECT *
          FROM fruits";

$result = @mysql_query ($query); //runs the query
if ($result) { //if it ran alright, display the records

    //load the JSAPI library

    echo '<table align="center" cellspacing="2" cellpadding="2">
          <tr><td align="left"><b>Name of fruit</b></td><td align="left"><b>Amount</b></td></tr>';

    //fetch and print all the records
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        echo "<tr><td align=\"left\">$row[0]</td><td align=\"left\">$row[1]</td></tr>
";
    }

    //load google visualization API library, piechart library and JSAPI library
    echo '<script type="text/javascript" src="https://www.google.com/jsapi"></script>
            <script type="text/javascript">

            google.load("visualization", "1.0",{"packages":["corechart"]});

            google.setOnLoadCallback(drawChart);

            function drawChart(){
                //create the data table
                var data = new google.visualization.DataTable();
                data.addColumn("string","Fruits");
                data.addColumn("number","Amount");
                data.addRows([["$row[0]","$row[1]"]]);

                //set chart options
                var options = {"title":"Amount of different fruits",
                    "width":400,
                    "height":300};

                //instantiate and draw chart, passing in options
                var options = new google.visualization.PieChart(document.getElementById("chart_div"));
                chart.draw(data, options);
                } //end of drawchart function
                </script>';

    //display chart
    echo '<div id="chart_div"></div>';

    echo '</table>';
    mysql_free_result($result); //free up the resources

} else{ //if it did not run alright
    echo '<p>The table could not be displayed due to a system error.</p><p>' . mysql_error() . '</p>';
}

mysql_close(); //close the database connection


?>
  • 写回答

1条回答 默认 最新

  • dqg17080 2013-05-13 13:00
    关注

    To use a variable inside strings, it must be in double-quotes "$var"

    It can be " '$var' ", but not ' "$var" '.

    If you don't want to rewrite your quotes / escape some of them you could try changing:

    data.addRows([["$row[0]","$row[1]"]]);

    To:

    data.addRows([
        ["'. $row[0] . '", "'. $row[1] . '"]
    ]);
    

    Also, you are not putting all results into js. To accomplish it you must store the results (here i just store them as strings):

    $jsRows = array() ;
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        $jsRows[] = "['{$row[0]}', '{$row[1]}']" ;
        echo "<tr><td align=\"left\">$row[0]</td><td align=\"left\">$row[1]</td></tr>
    ";
    }
    $jsRows = implode(", ", $jsRows) ;
    

    And then use that array in javascript like:

    echo '
        data.addRows([
            ' . $jsRows . '
        ]);
    ' ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题