title: Connecting Chart.js to MySQL database
body: <p>I have this script in my html document which creates a chart using Chart.js. The data in it are manualy inserted ( The labels and the data in datasets). The data in datasets are now randomly generated numbers. But I need to somehow connect it with my MySQL database.</p>
  
  <pre><code>&lt;script&gt;
     var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
     var barChartData = {
         labels : ["January","February","March","April","May","June","July","January","February","March","April","May","June","July"],
         datasets : [
            {
             fillColor : "rgba(23, 158, 3, 0.8)",
             strokeColor : "rgba(24, 107, 2, 0.8)",
             highlightFill: "rgba(24, 107, 2, 0.9)",
             highlightStroke: "rgba(24, 107, 2, 1)",
             data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
             }
             ]
     }
  window.onload = function(){
       var ctx2 = document.getElementById("canvas2").getContext("2d");
       ctx2.canvas.width = 1000;
       ctx2.canvas.height = 800;
       window.myBar = new Chart(ctx2).Bar(barChartData, {
              responsive : true
       });
  }
  </code></pre>
  
  <p></p>
  
  <p>I call select query in Model and then send the result to my View.
  And then in my View I can get to my data like this.
  I used a table as an example.</p>
  
  <pre><code> &lt;?php foreach ($this-&gt;list_excercise as $value) : ?&gt;
                    &lt;td&gt;&lt;?= $value['data'] ?&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;?= $value['label'] ?&gt;&lt;/td&gt;
   &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p>So the data can be inserted into html like this, but how can I insert it into chart.js javascript? So instead of </p>
  
  <pre><code>labels: ["January", "February"]
  </code></pre>
  
  <p>I would have something like</p>
  
  <pre><code>labels: $array
  </code></pre>
  
  <p>I cannot figure out a simple way of getting the data to the script. Can anyone help me out with this? Thank you in advance.</p>