0

首先很抱歉,这太业余了,我还是个初学者。

我正在练习与 PHP 中的数据库交互并使用 Google Visualization 很好地显示它。但是我的图表不会显示,我认为这是因为我如何将数据传递到图表中,因为我之前使用过 Google Charts,唯一不同的是使用 $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


?>
4

1 回答 1

1

要在字符串中使用变量,它必须用双引号括起来"$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 . '
    ]);
' ;
于 2013-05-13T13:00:33.167 回答