【发布时间】:2019-02-26 09:36:21
【问题描述】:
我正在做一个小项目,需要一些有关 PHP 查询的帮助。我本质上是在编写一个小型的 eLearning 类型的模块,所以当用户通过模块时,我会在检查点中写入。但是现在,我在 PHP myAdmin 中的表中手动放置值,我需要的是保存到 checkPointMod(检查点模块)表中的值,以传递到用户仪表板上的条形图/饼图。我只需要一个非常简单的查询,任何帮助都会很棒!
我尝试做某事(我可能完全偏离了轨道……):
$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
}
}]
}
}
});
【问题讨论】:
-
上述代码 sn-p 遇到什么问题?
-
您尝试了什么来发现错误?上面的 PHP 代码看起来你至少应该得到一个语法错误
-
检查您正在使用的图表的文档。我认为这里的 while 代码应该在 php 代码中。您可以使用 XHR 调用来填充数据数组或 javascript 文件或 php,就像您正在使用的那样
标签: php html bootstrap-4 chart.js