dow72046
dow72046
2013-05-13 12:48

如何将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 dqg17080 8年前

    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 . '
        ]);
    ' ;
    
    点赞 评论 复制链接分享

相关推荐