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 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题