我试图在条形图上显示每月的预订数量,我有两个单独的 sql 查询,我已经测试过它们并且都以正确的格式读回数据。但是,当我在浏览器中运行该文件时,没有任何显示,也没有在我的控制台中出现错误。下面是我的代码。
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "paperlesspub";
$conn = new mysqli($host, $user, $pass, $db);
$month = mysqli_query($conn, "SELECT MONTH(booking_date) FROM `booking`");
$sales = mysqli_query($conn, "select Month(booking_date) as Month, count(customer_ID) as Booking FROM booking GROUP BY month(`booking_date`)");
?>
这是我的图表代码
<script>
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: <?php while ($b = mysqli_fetch_array($month)) { echo '"' . $b['MONTH(booking_date)'] . '",';}?>],
datasets: [{
label: '# of Bookings',
data: <?php while ($p = mysqli_fetch_array($sales)) { echo '"' . $p['count(customer_ID'] . '",';}?>],
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
}
}]
}
}
});
</script>
如有任何建议,我们将不胜感激。
最佳答案
您的数组似乎缺少左括号 ([
)。
这里...
labels: <?php while ($b = mysqli_fetch_array($month)) { echo '"' . $b['MONTH(booking_date)'] . '",';}?>],
这里...
data: <?php while ($p = mysqli_fetch_array($sales)) { echo '"' . $p['count(customer_ID'] . '",';}?>],
应该是……
labels: [<?php while ($b = mysqli_fetch_array($month)) { echo '"' . $b['MONTH(booking_date)'] . '",';}?>],
和...
data: [<?php while ($p = mysqli_fetch_array($sales)) { echo '"' . $p['count(customer_ID'] . '",';}?>],
关于来自数据库的数量卷的 Javascript 条形图未显示在 php 网页中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54421477/