0

I have a javascript file with some graph information on it and where you see the tag 'data', this is where I want to write some php code in, so the values for the graph is taken from my table in phpMyAdmin. I'm quite a newby so any suggestions would be great.

The piechart.js code:

    var ctx = document.getElementById("myChart");

var myDoughnutChart = new Chart(ctx, {
    type: 'doughnut',
    data: {
        labels: ["Time Management", "Career Coach", "Stress & Wellbeing", "Note Taking", "Exam Prep", "Presentations"],
        datasets: [{
            label: 'Module Tracker',
            data: [6, 4, 2, 0, 3, 1],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
});

Where you see 6, 4, 2, 0, 3, 1 <- Is where I want to add in something like the below, so the values are taken directly from what I have in my tables:

$query = "SELECT * FROM `checkPointMod`";

$result = $conn -> query($query);

while($row = $result -> fetch_assoc())
  {
    echo $row['mod1']."<br>";
    echo $row['mod2']."<br>";
    echo $row['mod3']."<br>";
    echo $row['mod4']."<br>";
    echo $row['mod5']."<br>";
    echo $row['mod6']."<br>";
  }
$conn -> close();

enter image description here

Sunny0101
  • 444
  • 1
  • 5
  • 18
  • You could do an htaccess rule so the file loads a `php` server side but browser side makes a `js` request. I'd probably use AJAX though to just populate that data in the JS file. – user3783243 Mar 01 '19 at 12:36
  • 1
    No, you can't write PHP in js file. But you can use Ajax to achieve this – Zainul Abideen Mar 01 '19 at 12:36

0 Answers0