用php和mysql进行GoogleCharts

时间:2017-09-05 06:52:15

标签: php mysql charts

问候stackoverflow! 我现在把手放在图表上,特别是谷歌图表和使用php和phpmyadmin,我试图使用普通查询和结果集从数据库(课程名称+饼图的数字)中提取结果,但是我我只是在循环时获得一行,我会在下面发布我的代码,以便在可能的情况下获得一些提示。 (固定,检查第一个答案)

<?php
   include('../session.php');  
include("../config.php");
   ?>

<?php  




$sqlMostTakenCourse="SELECT course_title, count( * )
FROM wp_wpcw_user_courses, wp_wpcw_courses
WHERE wp_wpcw_user_courses.course_id = wp_wpcw_courses.course_id
AND wp_wpcw_user_courses.course_progress >0
AND wp_wpcw_user_courses.user_id
IN (

SELECT wp_users.ID
FROM wp_users, wp_usermeta, jdashboard
WHERE wp_usermeta.meta_key = 'CouponCode'
AND jdashboard.username = 'jad'
AND wp_usermeta.meta_value = jdashboard.coupon
AND wp_usermeta.user_id = wp_users.ID
)
GROUP BY course_title
ORDER BY count( * ) DESC ";



mysqli_set_charset($db,"utf8");

$resultMostTakenCourse = mysqli_query($db, $sqlMostTakenCourse);



   if(mysqli_num_rows($resultMostTakenCourse) > 0){

        while($row = mysqli_fetch_array($resultMostTakenCourse)){



  ?>


<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {

        var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day' ],

          [   ' <?php   echo $row[0];  ?>', <?php    echo $row[1];      ?>     ],

          ['Sleep',    7]
        ]);
<?php }} ?>
        var options = {
          title: 'My Daily Activities'

        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart'));

        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="piechart" style="width: 900px; height: 500px;"></div>
  </body>
</html>

[ ' <?php echo $row[0]; ?>', <?php echo $row[1]; ?> ],从数据库中只拉出一行,我将查看我的代码以查看缺少的内容,并感谢一些帮助和提示。  编辑:我意识到循环不是创建['名称',数字]取决于行,将尝试解决这个问题

1 个答案:

答案 0 :(得分:0)

我使用data.addRow()函数修复了问题并在循环之前初始化变量,修改后的代码如下:

<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Course');
data.addColumn('number', 'Count');

<?php 



        while($row = mysqli_fetch_array($resultMostTakenCourse)){



  ?>


 data.addRow(['<?php echo $row[0]; ?>', <?php echo $row[1]; ?>]);

        <?php } ?>

        var options = {
          title: 'Courses / Registered times'

        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart'));

        chart.draw(data, options);
      }