0

I am attempting to use google charts by providing it with data from a database using PHP. Been working for a few hours on it with a collection of code here and there and got some success using the following code.

                //Array
                $numData[] = "['First','Second']";

                //Query
                $query ="SELECT * FROM numbers";
                $statement = $db->query($query);

                //Loop to fetch data from database
                while($row = $statement->fetch(PDO::FETCH_ASSOC))
                {

                    $firnum = $row["First"];
                    $secnum = $row['Second'];

                    $numData[] = "['".$firnum."',".$secnum."]";

                    //debug see what data is being retrieved
                    echo $firnum . '<br />';
                    echo $secnum . '<br />';

                }

Then I'm adding this to the google charts drawchart() part of the code

// Create the data table.
                echo "var data = google.visualization.arrayToDataTable([";

                echo(implode(",",$numData));

                echo "]);";

Everything works fine, the chart is created however with no data in it. When testing the array it appears to be blank. The desired output would be to have the first and second numbers in a google chart via this array.

I have only just started learning php so apologies for any poor code. Any help to solve the issue behind this would be fantastic.

Cheers

The Codesee
  • 3,714
  • 5
  • 38
  • 78
user3212628
  • 27
  • 2
  • 10
  • side note: you should consider using var_dump() instead of echo for debugging because it shows you what data type you're dealing with. – devlin carnate May 25 '16 at 17:35
  • Possible duplicate of [Google Chart Tools with PHP & MySQl](http://stackoverflow.com/questions/7685745/google-chart-tools-with-php-mysql) – devlin carnate May 25 '16 at 17:44
  • Do you have to use JSON to implement google charts with php? – user3212628 May 25 '16 at 17:49
  • you don't have to. but if you use arrayToDataTable(), you need to use the object literal notation or specify the columns. Please see the [documentation](https://developers.google.com/chart/interactive/docs/reference#arraytodatatable). – devlin carnate May 25 '16 at 18:00

0 Answers0