Jquery Chart + PHP Two-Dimensional Arrays

Asked

Viewed 56 times

0

This question is a bit repetitive however I tried to reformulate the code in a different way.

So this is the following person, I performed a database query to fetch the values of the tags and the count and saved in a two-dimensional vector both fields. Then I made json_encode 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(tags) to know if it was working and 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 Alert will generate a graph with values ['Work', 5], ['Newthing', 5], ['Newthingz', 5] within the date variable. What I intended was to change these values to the two-dimensional array previously created "tags". To make something of it :

  var data = google.visualization.arrayToDataTable([
  ['Task', 'Hours per Day'],

  for (){
  imprimir cada posição do array tags
}

]);

Here’s the whole code :

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

<script type="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>

  • Please help xD, I’ve been trying to solve this code for hours.

  • Not even my teacher knows :/

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.