首先,很抱歉,我是如此业余,我仍然是一个初学者。

我正在练习与PHP中的数据库进行交互,并使用Google Visualization很好地显示它。但是我的图表无法显示,我想这是因为我将数据传递到图表中的原因,因为我之前使用过Google图表,而我要做的唯一不同的就是使用$ row将信息放入图表。

还是我走错路了,应该将$ row放入一个新数组,然后将其传递到图表中?

非常感谢!

这是我的代码:

<?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>\n";
    }

    //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


?>

最佳答案

要在字符串中使用变量,必须在双引号"$var"

它可以是" '$var' ",但不能是' "$var" '

如果您不想改写引号/转义某些引号,可以尝试更改:

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

至:

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


另外,您并没有将所有结果放入js。
为此,您必须存储结果(这里我只是将它们存储为字符串):

$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>\n";
}
$jsRows = implode(", ", $jsRows) ;


然后在javascript中使用该数组,例如:

echo '
    data.addRows([
        ' . $jsRows . '
    ]);
' ;

关于php - 如何将PHP数组嵌入到Google图表脚本中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16522463/

10-16 13:28