应领导要求,为了能看到每月反馈报告中一共有多少条反馈,同时有多少条未处理,前面提到了,更改数据库表结构,更新PHP上传表单代码,下面就是使用PHP的sql统计代码,来实现汇总统计。
思路如下:通过group by来按月分组,然后通过count来统计一共有多少条数量,但是期间发现一个问题,就是count好像是统计行数的,不管字段是不是为空,这样导致无法统计反馈中未处理的条数,于是更换思路和数据库字段,通过sum来统计未处理的反馈数量,具体参考代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php include_once("con.php"); $json = ''; $data = array(); class Ctfbdetails { } $array = array(); if(isset($_GET['monthrp'])){ $sql = "SELECT ctfb_month, count(ctfb_details) as fbnumber,sum(ctfb_actodo) as todonumber FROM fmc_ctfeedback group by ctfb_month "; //通过group by进行月份分组,然后统计ctfb_details字段的行数,因为前面提交表单时设置不能为空,所以用count统计,然后通过sum来求得ctfb_actodo字段的和,该字段如果未处理,则为1,处理了,则变为0 $result = $con->query($sql); if($result){ while ($row = mysqli_fetch_array($result,MYSQL_ASSOC) ) { $ctfbdetails = new Ctfbdetails (); $ctfbdetails->ctfb_month= $row["ctfb_month"]; $ctfbdetails->fbnumber= $row["fbnumber"]; $ctfbdetails->todonumber= $row["todonumber"]; $data[]=$ctfbdetails; } $json = json_encode($data);//把数据转换为JSON数据. echo "{".'"ctfbdetails"'.":".$json."}"; }else{ echo "查询失败", $con ->error; } } ?> |
这样就能将统计到的数据汇总,得出的数据如下:
1 |
{"ctfbdetails":[{"ctfb_month":"2019\u5e7404\u6708","fbnumber":"2","todonumber":"2"},{"ctfb_month":"2019\u5e7405\u6708","fbnumber":"9","todonumber":"6"}]} |
这样就能通过小程序读取并显示了,然后通过页面传值,读取ctfb_month相应的详细数据了。
最新评论