This question is a bit repetitive however I tried to rephrase the code in a different way.
So it's the folks next, I ran a database query to fetch tag and count values, and I saved both fields in a two-dimensional array. Then I made json_encode to be able to pass this array to a javascript variable.
<?php
$sql2 = "SELECT tag,contador FROM tags";
$result2 = $mysqli->query($sql2);
while ($row2 = $result2->fetch_assoc())
{
$array[][]= $row2["tag"];
$array[][]= $row2["contador"];
}
$js_array = json_encode($array);
echo "var tags = ". $js_array. ";\n";
?>
Then in javascript I showed an alert to see if it was working and it was correct! The values appeared this way (Example: Technology, 4).
alert(tags);
// Load google charts
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
// Draw the chart and set the chart values
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 5],
['Newthing', 5],
['Newthingz', 5]
]);
// Optional; add a title and set the width and height of the chart
var options = {'title':'Tag Ranks', 'width':450, 'height':400};
// Display the chart inside the <div> element with id="piechart"
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
This code above the alert will generate a graph with the values ['Work', 5], ['Newthing', 5], ['Newthingz', 5] within the variable date. What I intended was to change these values for the two-dimensional array created earlier "tags". To stay something of the genre:
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
for (){
imprimir cada posição do array tags
}
]);
Here is the whole code:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script><scripttype="text/javascript">
<?php
$sql2 = "SELECT tag,contador FROM tags";
$result2 = $mysqli->query($sql2);
while ($row2 = $result2->fetch_assoc())
{
$array[][]= $row2["tag"];
$array[][]= $row2["contador"];
}
$js_array = json_encode($array);
echo "var tags = ". $js_array. ";\n";
?>
alert(tags);
// Load google charts
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
// Draw the chart and set the chart values
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 5],
['Newthing', 5],
['Newthingz', 5]
]);
// Optional; add a title and set the width and height of the chart
var options = {'title':'Tag Ranks', 'width':450, 'height':400};
// Display the chart inside the <div> element with id="piechart"
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
<div id="piechart"></div>