title: Display items from the database in a chart
body: <p>I want to display the average of results for each exam paper in a graph. my problem is that when I catch the data from my database I stored it in an array so that I couldn't print these values as a list of array such as  <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;]</code> or <code>[44,100,50.29,100]</code> to use it in var xValues and var yValues , I also tried to use <code>foreach</code>, but it doesn't work. Please help.</p>
  <p>/* Here is JS code  */</p>
  <pre><code>&lt;script type=&quot;text/javascript&quot; src=&quot;https://www.gstatic.com/charts/loader.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js&quot;&gt;&lt;/script&gt;
  
  &lt;script&gt;
      var xValues =&lt;?php foreach($examPapertitle as $title) {
                          echo $title;
                          }; ?&gt;
                          
      var yValues =&lt;?php foreach($avgcountresult as $average) {
                          echo $average;
                          }; ?&gt;
      var barColors = [&quot;red&quot;, &quot;green&quot;,&quot;blue&quot;,&quot;orange&quot;,&quot;brown&quot;];
  
      new Chart(&quot;myChart&quot;, {
        type: &quot;bar&quot;,
        data: {
          labels: xValues,
          datasets: [{
            backgroundColor: barColors,
            data: yValues
          }]
        },
        options: {
          legend: {display: false},
          title: {
            display: true,
            text: &quot;Average of Results&quot;
          }
        }
      });
  &lt;/script&gt;
  </code></pre>
  <p>/* Here is HTML Code */</p>
  <pre><code>$examPapertitle=array();
  $avgcountresult= array();
  $fetchresult = mysqli_query($conn, &quot;SELECT AVG(results.percentage), exam_paper.examTitle FROM results INNER JOIN exam_paper ON results.examPaperID = exam_paper.examPaperID GROUP by results.examPaperID ORDER by results.examPaperID&quot;);
  while($rowresult= mysqli_fetch_assoc($fetchresult)){
      $examPapertitle[] = $rowresult['examTitle'];
      $avgcountresult[] = $rowresult['AVG(results.percentage)'];
  }
  
  
  &lt;canvas id=&quot;myChart&quot; style=&quot;width:100%;max-width:600px&quot;&gt;&lt;/canvas&gt;
  </code></pre>