The Charts documentation uses the following example:
var data = new google.visualization.DataTable();
data.addColumn('string', 'Toppings');
data.addColumn('number', 'Slices');
data.addRows([
['Cheese', 3],
['Onions', 1],
['Olives', 1],
['Zucchini', 1],
['Pepperoni', 2]
]);
I'm building my Google Chart as follows:
var data = new google.visualization.DataTable();
data.addColumn('string', 'Standing');
data.addColumn('number', 'Students');
data.addRows([
[<?= $users[0]->COLLEGE; ?>, <?= $users[0]->HEADCOUNT; ?>],
]);
But no chart is being printed. How can I use the PHP array contents to populate the chart?
Your code doesn't add quotes to the resulting JavaScript values (and you don't escape them). PHPs json_encode has the ability to produce valid JavaScript values from PHP strings and handle all the quotes and escaping for you so just use that:
var data = new google.visualization.DataTable();
data.addColumn('string', 'Standing');
data.addColumn('number', 'Students');
data.addRows([
[<?= json_encode($users[0]->COLLEGE); ?>, <?= json_encode($users[0]->HEADCOUNT, JSON_NUMERIC_CHECK); ?>],
]);
That should also prevent a whole bunch of XSS and other nasty code injection attacks and handle escaping.