简体   繁体   English

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

[英]I need a PHP query to grab data from the database and use in my bar charts for results

I'm working on a small project and need some help with a PHP query. 我正在做一个小项目,需要有关PHP查询的帮助。 I essentially am writing a small eLearning type module, so as the user goes through the module, I will write in check points. 我实质上是在编写一个小型的eLearning类型的模块,因此当用户浏览该模块时,我将在检查点中进行编写。 However for now, I'm manually placing values inside my table in PHP myAdmin, what I need is the values that are saved into the checkPointMod (check point module) table, to be passed to my bar/pie charts on a users dashboard. 但是,现在,我要在PHP myAdmin中的表中手动放置值,我需要的是保存到checkPointMod(检查点模块)表中的值,以传递给用户仪表板上的条形图/饼图。 I just need a really simple query, any help would be great!! 我只需要一个非常简单的查询,任何帮助都会很棒!

我的桌子的屏幕截图:

My attempt at trying something (which I may be completely off rails here...): 我尝试尝试某些事情(在这里我可能会完全脱离轨道……):

    $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();

My pie chart code which works (with manually placing in the data values): 我的饼图代码有效(手动放置数据值):

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
                }
            }]
        }
    }
});

Generally, what you have is correct, I tend to write my queries seperate from the chart to maintain seperation; 通常,您所拥有的是正确的,我倾向于将查询与图表分开记录,以保持分隔。 eg: 例如:

<?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();
?>

You can do a print_r to check the content of your array, if need be ... 如果需要,可以执行print_r检查数组的内容。

Then in your chart section: 然后在图表部分:

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

Hope that helps you get on you way ... 希望能帮助您继续前进...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM