简体   繁体   English

如何循环 php mysqli_query 以将数据输入谷歌组合图表

[英]How to loop php mysqli_query as to input data into google combochart

I am currently working on a college management system and want to display a combo chart regarding the total number of students enrolment for each course per month.我目前正在开发一个大学管理系统,并希望显示一个关于每月每门课程的学生总数的组合图表。

In my case, there are:就我而言,有:

  • 3 Courses: DICS, DIHM and DIBA 3 门课程:DICS、DIHM 和 DIBA
  • 6 Months: 072019 until 122019 6 个月:072019 至 122019

What I currently did is by creating the 6 queries of 6 months each for DICS, and create another 6 for DIHM and DIBA respectively, which I know is a stupid and insufficient way.我目前所做的是为 DICS 创建 6 个每个 6 个月的查询,并分别为 DIHM 和 DIBA 创建另外 6 个,我知道这是一种愚蠢且不充分的方法。

Here is how my graph will look like: My combo chart这是我的图表的样子:我的组合图

Here are source code of combo graph: Google chart以下是组合图的源代码: 谷歌图表

Here is my sql query for first 3 months as example:这是我前 3 个月的 sql 查询示例:

$DICS_users_072019 = "SELECT * FROM users WHERE course = 'DICS' AND enroll_month = 072019";
$select_DICS_072019 = mysqli_query($connection,$DICS_users_072019);  
$total_DICS_072019 = mysqli_num_rows($select_DICS_072019);
echo $total_DICS_072019;

$DICS_users_082019 = "SELECT * FROM users WHERE course = 'DICS' AND enroll_month = 092019";
$select_DICS_082019 = mysqli_query($connection,$DICS_users_082019);  
$total_DICS_082019 = mysqli_num_rows($select_DICS_082019);
echo $total_DICS_082019;

$DICS_users_092019 = "SELECT * FROM users WHERE course = 'DICS' AND enroll_month = 092019";
$select_DICS_092019 = mysqli_query($connection,$DICS_users_092019);  
$total_DICS_092019 = mysqli_num_rows($select_DICS_092019);
echo $total_DICS_092019;

This is how I insert dynamic data into combo chart as input:这就是我将动态数据作为输入插入组合图表的方式:

 <script type="text/javascript">
 google.charts.load('current', {'packages':['corechart']});
 google.charts.setOnLoadCallback(drawVisualization);

     function drawVisualization() {
     // Some raw data (not necessarily accurate)
        var data = google.visualization.arrayToDataTable([
              ['Month',   'DIHM',   'DICS',      'DIBA'],   
        <?php

        $enrol_month = ['07/2019','08/2019','09/2019'];
        $DICS_nums = [$total_DICS_072019 , $total_DICS_082019 , $total_DICS_092019];
        $DIHM_nums = [$total_DIHM_072019 , $total_DIHM_082019 , $total_DIHM_092019];
        $DIBA_nums = [$total_DIBA_072019 , $total_DIBA_082019 , $total_DIBA_092019];

        for($i = 0;i< 3;i++){

        echo "['{$enrol_month[$i]}'" . "," . "'{$DICS_nums[$i]}'" . "'{$DIHM_nums[$i]}'" . "'{$DIBA_nums[$i]}']";

        }

        ?>
                ]);

        var options = {
           title : 'Total Admission of Students per Courses',
           vAxis: {title: 'Numbers'},
           hAxis: {title: 'Month'},
           seriesType: 'bars',
           series: {5: {type: 'line'}}        };

           var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
              chart.draw(data, options);
           }
</script>       

So is there any way to make it easier?那么有没有什么办法可以让它变得更简单呢? I was thinking to put it into foreach loop and doing things like "WHERE course = '{x}' AND enrol_month = '{y}';"我正在考虑将其放入 foreach 循环中并执行诸如“WHERE course = '{x}' AND enrol_month = '{y}';”之类的事情and store the result into an associative array.并将结果存储到关联数组中。 Is it valid way to done this?这是有效的方法吗?

for the query, you can group by month, and use a case statement for each column.对于查询,您可以按月份分组,并对每一列使用case语句。

SELECT
  enroll_month,
  sum(case when course = 'DIHM' then 1 else 0 end) as DIHM,
  sum(case when course = 'DICS' then 1 else 0 end) as DICS,
  sum(case when course = 'DIBA' then 1 else 0 end) as DIBA
FROM
  users
GROUP BY
  enroll_month

// build query
$query = "SELECT enroll_month, ";
$query .= "sum(case when course = 'DIHM' then 1 else 0 end) as DIHM, ";
$query .= "sum(case when course = 'DICS' then 1 else 0 end) as DICS, ";
$query .= "sum(case when course = 'DIBA' then 1 else 0 end) as DIBA ";
$query .= "FROM users ";
$query .= "GROUP BY enroll_month ";

then load the data into an array...然后将数据加载到数组中......

// build data table
$table = [];
$table[] = ['Month', 'DIHM', 'DICS', 'DIBA'];

// get data
$result = mysqli_query($connection,$query);
while($row = mysqli_fetch_array($result)) {
  // add row to data table
  $table[] = [$row["enroll_month"], $row["DIHM"], $row["DICS"], $row["DIBA"]];
}

and create the data table...并创建数据表...

var data = google.visualization.arrayToDataTable(<?php echo json_encode($table); ?>);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM