在chart.js中输出多个SQL查询

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

因此,我在chart.js中创建了一个图表,该图表应在图表中输出我的sql查询结果,但是在运行代码时,图表的每个部分每次都显示“ 1”。因此,如果代码中显示的查询图表中的部分应为12,2,5,9,11,则它仅显示1,1,1,1,1。知道我哪里出错了吗?

  <script>
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
  type: 'doughnut',
  data: {
    labels: ['Body Pump', 'Spin Class', 'Yoga', 'Body Tone', 'Legs, Bums & Tums'],
    datasets: [{
      label: 'Total Number of Bookings',
      data: [
      <?php

      $resultbodypump = mysqli_query($db, "SELECT COUNT(SessionType) FROM bookings WHERE username = '".$_SESSION['username']."' AND SessionType = 'bodyPump'");

      $resultspinclass = mysqli_query($db, "SELECT COUNT(SessionType) FROM bookings WHERE username = '".$_SESSION['username']."' AND SessionType = 'spinClass'");

      $resultyoga = mysqli_query($db, "SELECT COUNT(SessionType) FROM bookings WHERE username = '".$_SESSION['username']."' AND SessionType = 'yoga'");

      $resultbodytone = mysqli_query($db, "SELECT COUNT(SessionType) FROM bookings WHERE username = '".$_SESSION['username']."' AND SessionType = 'bodyTone'");

      $resultlegsbumstums = mysqli_query($db, "SELECT COUNT(SessionType) FROM bookings WHERE username = '".$_SESSION['username']."' AND SessionType = 'legsBumsTums'");

      echo mysqli_num_rows($resultbodypump) . ",";
      echo mysqli_num_rows($resultspinclass) . ",";
      echo mysqli_num_rows($resultyoga) . ",";
      echo mysqli_num_rows($resultbodytone) . ",";
      echo mysqli_num_rows($resultlegsbumstums);

      ?>
      ],
php sql chart.js
1个回答
0
投票
  <script>
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
  type: 'doughnut',
  data: {
    labels: ['Body Pump', 'Spin Class', 'Yoga', 'Body Tone', 'Legs, Bums & Tums'],
    datasets: [{
      label: 'Total Number of Bookings',
      data: [
      <?php

      $querybodypump = "SELECT COUNT(SessionType) FROM bookings WHERE username = '".$_SESSION['username']."' AND SessionType = 'bodyPump'";
      $resultbodypump = mysqli_query($db,$querybodypump);
      $row1 = mysqli_fetch_row($resultbodypump);
      $count1 = $row1[0];
      $queryspinclass = "SELECT COUNT(SessionType) FROM bookings WHERE username = '".$_SESSION['username']."' AND SessionType = 'spinClass'";
      $resultspinclass = mysqli_query($db,$queryspinclass);
      $row2 = mysqli_fetch_row($resultspinclass);
      $count2 = $row2[0];
      $queryyoga = "SELECT COUNT(SessionType) FROM bookings WHERE username = '".$_SESSION['username']."' AND SessionType = 'yoga'";
      $resultyoga = mysqli_query($db,$queryyoga);
      $row3 = mysqli_fetch_row($resultyoga);
      $count3 = $row3[0];
      $querybodytone = "SELECT COUNT(SessionType) FROM bookings WHERE username = '".$_SESSION['username']."' AND SessionType = 'bodyTone'";
      $resultbodytone = mysqli_query($db,$querybodytone);
      $row4 = mysqli_fetch_row($resultbodytone);
      $count4 = $row4[0];
      $querylegsbumstums = "SELECT COUNT(SessionType) FROM bookings WHERE username = '".$_SESSION['username']."' AND SessionType = 'legsBumsTums'";
      $resultlegsbumstums = mysqli_query($db,$querylegsbumstums);
      $row5 = mysqli_fetch_row($resultlegsbumstums);
      $count5 = $row5[0];

      echo $count1. ",";
      echo $count2 . ",";
      echo $count3 . ",";
      echo $count4 . ",";
      echo $count5 ;

      ?>
      ],
© www.soinside.com 2019 - 2024. All rights reserved.