0

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


?>
1
  • As the answers below suggest, it's likely that your problem is in the data format. There is a good chance that if you use chrome dev tools or firebug and console.log the data, then the error will be apparent Commented May 13, 2013 at 23:32

1 Answer 1

1

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

And then use that array in javascript like:

echo '
    data.addRows([
        ' . $jsRows . '
    ]);
' ;
Sign up to request clarification or add additional context in comments.

1 Comment

I just rewrote my quotes, but the chart is still nowhere to be seen. Do you think it's a problem with $row? Should I try and pass $row into a new array and then use that one? Thanks btw!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.