我需要一个PHP查询来从数据库中获取数据,并在条形图中使用结果

问题描述 投票:0回答:1

我正在开发一个小项目,需要一些PHP查询帮助。我基本上正在编写一个小型电子学习类型模块,因此当用户通过该模块时,我将写入检查点。但是现在,我在PHP myAdmin中手动将值放在我的表中,我需要的是保存到checkPointMod(检查点模块)表中的值,以传递到用户仪表板上的条形图/饼图。我只需要一个非常简单的查询,任何帮助都会很棒!!

Screenshot of my table:

我试图尝试某些东西(我可能完全脱离轨道......):

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

echo $query;


//3.Execute -----------
$result = $conn -> query($query);



while($row = $result -> fetch_assoc())  // <- While there are still results, fetch those using the assoc array, which creates an array of results from the database using the fields as headings. The arrow is related to OO, it's similar to pass the query on the left to the parameter to the right
  {
    data: [<?php echo $row['mod1']; ?>, <?php echo $row['mod2']; ?>, <?php echo $row['mod3']; ?>, <?php echo $row['mod4']; ?>, <?php echo $row['mod5']; ?>],   //When you add the "" around a html tag, you don't need to open <html> & close.
  }



//4.Disconnect ---------
$conn -> close();

我的饼图代码有效(手动放入数据值):

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
                }
            }]
        }
    }
});
php html bootstrap-4 chart.js
1个回答
1
投票

一般来说,你所拥有的是正确的,我倾向于将我的查询与图表分开来保持分离;例如:

<?php
$query="SELECT * FROM checkPointMod";
# echo $query;
$newArray = array();
$result = $conn->query($query);
while($row = $result -> fetch_assoc());  {
    $newArray[] = $row['mod1'];
    $newArray[] = $row['mod2'];
    $newArray[] = $row['mod3'];
    $newArray[] = $row['mod4'];
    $newArray[] = $row['mod5'];
}
$conn -> close();
?>

如果需要,您可以执行print_r检查数组的内容...

然后在你的图表部分:

 data: [<?= $newArray ?>],

希望能帮助您顺利前进......

© www.soinside.com 2019 - 2024. All rights reserved.