简体   繁体   English

Morris.js条形图的数据来自php数据库循环

[英]Data for the Morris.js bar graph is from a php database loop

I have this PHP code: 我有这个PHP代码:

$number = mysqli_num_rows(mysqli_query($db_conn, "Select * from barangay"));
for ($i=1; $i<=$number; $i++){
$chart_fetch=mysqli_query($db_conn, "Select * from health where BRGY_id=$i");
  $chart_data[$i] = "";
    while($chart=mysqli_fetch_array($chart_fetch)){
      $chart_data[$i] .= "{year:'".$chart["year"]."', pneumonia:".$chart["pneumonia"].", asthma:".$chart["asthma"].", tuberculosis:".$chart["tuberculosis"]."}, ";
    }
    $chart_data[$i] = substr($chart_data[$i], 0, -2);
}

and for the chart script: 对于图表脚本:

<script>
        var max = <?php echo $number; ?>;
        for (i=1; i<=max; i++){
          Morris.Bar({
            element: 'chart'+i,
            data:[ <?php echo $chart_data[$i]; ?>],
            xkey:'year',
            ykeys:['pneumonia',  'asthma', 'tuberculosis'],
            labels:['Pneumonia', 'Asthma', 'Tuberculosis'], //label for the pop-up key
            hideHover:'auto',
            barColors: ['#036016', '#009f29', '#03440C']
          });
       }
      </script>

I can't seem to get the chart_data[$i] to keep on adding one value so that i could get the fetched data from the php script as I had stated above. 我似乎无法让chart_data [$ i]继续添加一个值,这样我就可以像上面所说的那样从php脚本中获取数据。

The output of my codes must look like this: For Barangay 1 For Barangay 2 我的代码的输出必须如下所示: 对于Barangay 1 对于Barangay 2

Because I cannot create loop or concatenate the script variable i to the php code because of the client and server issue. 由于客户端和服务器问题,由于无法创建循环或将脚本变量i连接到php代码,因此无法执行。

I already got the answer to my problem. 我已经有了解决我问题的答案。 All I have to do was just to insert the script inside the php code, and let php do the looping for me. 我要做的只是将脚本插入php代码,然后让php为我执行循环。

I should have known it sooner. 我早该知道的。 :) :)

But anyway, here's the code to help others with cases such as this. 但是无论如何,这是在诸如此类的情况下可以帮助他人的代码。

<?php 
        $number1 = mysqli_num_rows(mysqli_query($db_conn, "Select * from barangay"));
          for ($z=1; $z<=$number1; $z++){
            $z=$z;
            echo"<script>
              var i =1;
              var max= $number1;
                Morris.Bar({
                  element: chart$z,
                  data:[$chart_data[$z]],
                  xkey:'year',
                  ykeys:['pneumonia',  'asthma', 'tuberculosis'],
                  labels:['Pneumonia', 'Asthma', 'Tuberculosis'],
                  hideHover:'auto',
                  barColors: ['#036016', '#009f29', '#03440C']
                });
            </script>
            ";
          }
      ?>

And the output would be different bar graphs which are getting their data from the database. 并且输出将是不同的条形图,这些条形图将从数据库中获取其数据。 <3 Like this, (this is from a 67% zoom from my web browser so you could see 2 of my 7 graphs having different data fetched from the database): Bar Graph 1 and 2 <3这样,(这是通过Web浏览器进行的67%缩放,因此您可以看到我的7个图中有2个具有从数据库中提取的不同数据): 条形图1和2

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

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