[英]Retrieving data from the database and displaying it in a Line Chart on a webpage
[英]Javascript Bar chart with number volumes from database not displaying in php webpage
我試圖在條形圖上顯示每月的預訂數量,我已經測試了兩個單獨的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'] . '",';}?>],
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.