Php Codeigniter的月度报告
我正在开发一个项目,这是一个酒店预订系统 我的问题是我想每个月循环一次,比如1月是1月2日是2月,依此类推。如果是1,则通过将其与mySQL数据库表上创建的Php Codeigniter的月度报告,php,mysql,codeigniter,Php,Mysql,Codeigniter,我正在开发一个项目,这是一个酒店预订系统 我的问题是我想每个月循环一次,比如1月是1月2日是2月,依此类推。如果是1,则通过将其与mySQL数据库表上创建的cout\u列进行比较,获得月份为1或一月的所有行,并添加每条记录的所有利润,稍后将其放入如下数组中: $monthlyreport = [ 1 => 6000, 2 => 5000, 3 => 3000, 4 => 12000, 5 => 8000, 6 => 4000
cout\u
列进行比较,获得月份为1或一月的所有行,并添加每条记录的所有利润,稍后将其放入如下数组中:
$monthlyreport = [
1 => 6000,
2 => 5000,
3 => 3000,
4 => 12000,
5 => 8000,
6 => 4000,
7 => 6000,
8 => 9000,
9 => 4000,
10 => 6000,
11 => 9000,
12 => 4000,
];
这是示例行的mySQL数据库表模式:
Row Name Row Data
id 9
gst_id 1
cout_tin 2018-07-01
cout_tout 2018-07-02
cout_nodays 1
cout_single 1
cout_double 2
cout_family 1
cout_roombal 13000
cout_billbal 1120
cout_totalbal 14120
cout_downbal 6500
cout_result 7620
cout_created 2018-07-15 09:34:12
我将PHP与Codeigniter框架一起使用。您需要做的是将数据库查询与PHP中的简单for循环相结合 首先,您必须了解如何获取月份内的所有值
cout\u created
是一个时间戳/日期时间字段。
在MySQL中,您可以使用如下查询获取数据:
从表中选择*月份(cout_created)=XX
使用此查询,在PHP中,您将执行以下操作:
// Initialize the array
$monthlyreport = array();
// For each month we are gonna do the same
for ($month = 1; $month <= 12; $month++) {
// We get the results with database library, changing the sql according to our needs.
$sql = "SELECT cout_result FROM yourtable WHERE MONTH(cout_created) = " . $month;
$query = $this->db->query($sql);
// The accum variable to sum all the profits.
$sum = 0;
// And foreach record, we sum the value to the actual one.
foreach($query->result() as $row) {
$sum = $sum + $row->cout_result;
}
// When finish, save the result on the array and start again.
$montlyreport[$month] = $sum;
}
$currentyear\u result=array();
对于($i=1;$i db->其中('YEAR(created_at'),date('Y'))->其中('MONTH(created_at'),date($i))->统计所有结果('order_details');
如果(!空($year_record)){
$currentyear\u result[$i]=$lastyear\u record;
}否则{
$currentyear_结果[$i]=0;
}
}
输出:数组(
[1] => 8
[2] => 3
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 1
[10] => 0
[11] => 0
[12] => 0 )
这很难理解…首先我理解的是你正在使用一个数据库,它的结构与你在帖子中展示的一样…然后,你这个月要看什么?cout\u-created
?cout\u-tout
?这是MySQL吗?MongoDB?提供更多细节,我会帮你一把。这是我想的吗ng,这个解决方案很简单。它在你创建的法庭上,先生。这是一个时间戳,我使用的是MySQL。对不起,先生。好的,现在给你写答案:)谢谢你的时间,先生。试试看,告诉我它是否对你有效。@Alpha\u Bords,不客气。很高兴帮助你。
// Initialize the array
$monthlyreport = array();
// For each month we are gonna do the same
for ($month = 1; $month <= 12; $month++) {
// But now we will get sum of the values instead of each value
$sql = "SELECT SUM(cout_result) as profit FROM yourtable WHERE MONTH(cout_created) = " . $month;
$query = $this->db->query($sql);
// And just save the profit
$montlyreport[$month] = $query->row()->profit;
}
$sql = "SELECT SUM(cout_result) as profit, MONTH(cout_created) as mymonth FROM yourtable GROUP BY MONTH(cout_created)"
$query = $this->db->query($sql);
$monthlyreport = array();
foreach ($query->result() as $row) {
$monthlyreport[$row->mymonth] = $row->profit;
}
$currentyear_result=array();
for ($i = 1; $i <= 12; $i++) {
$year_record =$this->db->where('YEAR(created_at)', date('Y'))->where('MONTH(created_at)', date($i))->count_all_results('order_details');
if (!empty($year_record)) {
$currentyear_result[$i] =$lastyear_record;
} else {
$currentyear_result[$i] = 0;
}
}
Output: Array (
[1] => 8
[2] => 3
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 1
[10] => 0
[11] => 0
[12] => 0 )