English 中文(简体)
如数据不可用(CI、PHP、MySQL)
原标题:Display Yearly Report When Data Not Available (CI, PHP, MySQL)

I want to display a yearly report based on month, let say I have orders in January, August & December but for the rest of the year there are no orders.

当我问询使用 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 is only 3 rows returned.

Say I 然后做: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 )
      )

This is fine but I need to fill in the rest of the year with the missing months. If the month doesn t exist in the database I need to default the amount to zero.

基本上,如果该月不存在,我将用阿雷拉来最后,用从数据库中得出的数值或零。

最佳回答

I m not sure if this is possible to do in SQL. It could be.

这里是使用只有购买力平价的解决办法。

$orders = array(
    array(
         month_num  => 1,
         amount  => 4
    ),
    array(
         month_num  => 2,
         amount  => 0
    ),   
    array(
         month_num  => 3,
         amount  => 4
    )
);

$report = array_combine( range( 1, 12), array_fill( 1, 12, 0));
foreach( $orders as $order)
{
    $report[ $order[ month_num ] ] = $order[ amount ];
}

var_dump( $report);

如果你把钥匙作为月数和命令数目的价值(如上述答案)来使用,你也可以避免一个多层面阵列:

$orders = array( 1 => 4, 2 => 0, 3 => 0, 4 => 0);

之后,你可以按一行编写报告:

$report = array_replace( array_combine( range( 1, 12), array_fill( 1, 12, 0)), $orders);

Demo

问题回答
$month_array = Array ( [0] => Array ( [month_num] => 1 [amount] => 4 ) [1] => Array ( [month_num] => 8 [amount] => 1 ) [2] => Array ( [month_num] => 12 [amount] => 19 ) );

$new_month_array = array();
foreach( $month_array as $month ){
    $new_month_array[] = $month;
}




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签