codeigniter - Get month wise count from Mysql database even if there are no results -


i creating graph display month wise count mysql database.

execute following query:

$cash_query =  $this->db->query("select count(*) count_monthwise, month(from_unixtime(dt_added)) month `order` user_id='1'  , status != '9' , payment_type = '1' group month"); $cash_result = $cash_query->result(); 

output:

array (     [0] => stdclass object         (             [count_monthwise] => 1             [month] => 8         )      [1] => stdclass object         (             [count_monthwise] => 2             [month] => 9         )  ) 

in above output, there display "count_monthwise" means count , month "8" means "8th month - august".

but want display output months, if find count 0 in months, display [count_monthwise] => 0.

i want display exact output like:

    array     (         [0] => stdclass object             (                 [count_monthwise] => 1                 [month] => 1             )         [1] => stdclass object             (                 [count_monthwise] => 1                 [month] => 2             )             .             .             .             .             .             .             .          [10] => stdclass object             (                 [count_monthwise] => 0                 [month] => 11             )         [11] => stdclass object             (                 [count_monthwise] => 0                 [month] => 12             )      ) 

i have used using foreach loop this, not working.

loop

foreach($cash_result $cash => $cash_value){    for($i=0;$i<=11;$i++){       if($i == $cash){       }    } } 

that 1 way around:

select   coalesce(yourquery.count_monthwise,0) monthwise_count,  allmonths.month (select 1 month union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10 union select 11 union select 12 ) allmonths left join (  select    count(*) count_monthwise,    month(from_unixtime(dt_added)) month   `order`   user_id='1'  , status != '9' , payment_type = '1'   group month ) yourquery  on allmonths.month = yourquery.month 

Comments