Display Yearly Report When Data Not Available (CI, PHP, MySQL)

Posted by tegaralaga on Stack Overflow See other posts from Stack Overflow or by tegaralaga
Published on 2011-11-13T17:37:27Z Indexed on 2011/11/13 17:51 UTC
Read the original article Hit count: 271

Filed under:
|
|
|

First of all, i do apologize for my bad english, cos english isn't my native language.

I want to display yearly report based on month, let say i got order on January, August, December, but the rest there's no order. So in MySQL database only have 3 order (Jan,Aug,Dec).

When i query use CI ( select month(order_date) as month_name , count(order_id) as amount from order where year(order_date)=2011 group by month(order_date) ) there's only 3 data let say the 3 data is (use $query->result_array())

Array ( [0] => Array ( [month_num] => 1 [amount] => 4 ) [1] => Array ( [month_num] => 8 [amount] => 1 ) [2] => Array ( [month_num] => 12 [amount] => 19 ) )

how to make it to 12 data (12 Month) the array become like this (when data not available the amount is 0)

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

Thanks in advance :)

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql