douhao7889 2018-11-15 04:35
浏览 127

PHP:我需要在没有sql查询数据的月份填写“0”

This seemed simple at first, but now it has become a headache.

I have a number of robberies for each month in a year, lets say 2016. My query

SELECT MONTH(denuncia.fecha_registro_denuncia) as mes, count(denuncia.codigo_incidente)
            FROM denuncia
            INNER JOIN incidentes ON incidentes.codigo_incidente=denuncia.codigo_incidente
            WHERE YEAR(denuncia.fecha_registro_denuncia)='".$a."' AND denuncia.codigo_incidente=0
            GROUP BY mes

denuncia is the same as "report to the police"

returns this:

+-------+--------+
| month | robber.|
+-------+--------+
|     1 |      2 |
|     2 |      2 |
|     3 |      3 |
|     4 |      2 |
|     5 |      1 |
|     8 |      2 |
|     9 |      3 |
|    10 |      1 |
|    11 |      5 |
|    12 |      2 |
+-------+--------+

The query is fetched as an PHP multidimensional array, using

while($rows=mysqli_fetch_array($result,MYSQLI_NUM)) {
    $b[]=$rows;
}

Data in array format:

Array
(
[0] => Array
    (
        [0] => 1
        [1] => 2
    )

[1] => Array
    (
        [0] => 2
        [1] => 2
    )

[2] => Array
    (
        [0] => 3
        [1] => 3
    )

[3] => Array
    (
        [0] => 4
        [1] => 2
    )

[4] => Array
    (
        [0] => 5
        [1] => 1
    )

[5] => Array
    (
        [0] => 8
        [1] => 2
    )

[6] => Array
    (
        [0] => 9
        [1] => 3
    )

[7] => Array
    (
        [0] => 10
        [1] => 1
    )

[8] => Array
    (
        [0] => 11
        [1] => 5
    )

[9] => Array
    (
        [0] => 12
        [1] => 2
    )

)

As you can see, I am missing months 6 and 7 from my query result. Those months there were no incidents. As I searched through StackOverflow, filling the query with zeroes is somewhat a complex SQL query.

So, I have PHP to solve this. I need to output this data to a chart display library so I can view it nice and clean. The format for the input data would be:

[2,2,3,2,1,0,0,2,3,1,5,2]

So I need to put 0's on positions 6 and 7 to fill empty months with no incidents.

Thing is, my coding skills are poor. I tried some solutions but keep getting into a wall, been hours already.

Basically the algorythm is: -Go through the array -Check if month exists -If it doesn´t exist, echo '0,"; -Go next position -Check again if month exists -...

Been coding for many hours and my mind can´t grasp the solution. Thanks for any help.

  • 写回答

2条回答 默认 最新

  • dsymx68408 2018-11-15 04:50
    关注

    one way you can do is you can pree fill month array with zero and set value

    $month = array_fill_keys(range(1, 12),0); //fill month array key with month(1-12)
    
    while($rows=mysqli_fetch_array($result,MYSQLI_NUM)) {
      $month [$rows[0]]= $rows[1]; //check if exists set value
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私