繁体   English   中英

如何使用 PHP 和 MySql 对饼图进行分类

[英]How to Categorize a Pie Chart using PHP and MySql

我正在尝试使用饼图和 php 对费用进行分类。 但是,每次我输入费用时,它都会显示为另一个类别。

例如,如果在“娱乐”类别下有两项费用,则饼图上将有两个单独的部分,每个部分代表一项费用。

但是,我想将同一类别的所有费用组合在一起。

以下是我用来生成饼图的脚本。 谢谢!

function drawChart() {

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

      <?php

      $result1=$con->query("select amount from info where category='Housing' and user_id='$id'");
      while($row1=$result1->fetch_assoc()){
        echo "['Housing'," .$row1['amount']."],";
      }

      $result2=$con->query("select amount from info where category='Transportation' and user_id='$id'");
      while($row2=$result2->fetch_assoc()){
        echo "['Transportation'," .$row2['amount']."],";
      }

      $result3=$con->query("select amount from info where category='Food' and user_id='$id'");
      while($row3=$result3->fetch_assoc()){
        echo "['Food'," .$row3['amount']."],";
      }

      $result4=$con->query("select amount from info where category='Utilities' and user_id='$id'");
      while($row4=$result4->fetch_assoc()){
        echo "['Utilities'," .$row4['amount']."],";
      }

      $result5=$con->query("select amount from info where category='Medical/Healthcare'");
      while($row5=$result5->fetch_assoc()){
        echo "['Medical/Healthcare'," .$row5['amount']."],";
      }

      $result6=$con->query("select amount from info where category='Personal'");
      while($row6=$result6->fetch_assoc()){
        echo "['Personal'," .$row6['amount']."],";
      }

      $result7=$con->query("select amount from info where category='Education'");
      while($row7=$result7->fetch_assoc()){
        echo "['Education'," .$row7['amount']."],";
      }

      $result8=$con->query("select amount from info where category='Gifts/Donations'");
      while($row8=$result8->fetch_assoc()){
        echo "['Gifts/Donations'," .$row8['amount']."],";
      }

      $result9=$con->query("select amount from info where category='Entertainment'");
      while($row9=$result9->fetch_assoc()){
        echo "['Entertainment'," .$row9['amount']."],";
      }
       ?>
    ]);

请检查while中使用的比较运算符=是否有错误。

或者你可以简单地使用一个foreach循环?:

foreach ($result as $row) {
    ...
}

暂无
暂无

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

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