每月使用 codeigniter 统计数据

Count data Using codeigniter monthly wise

我正在使用 Codeigniter 3.0 和 sql 服务器

这是我的table

customers table:
Amount      Date                            Id
1.00      2014-01-05 00:00:00.000           1
2.00      2014-01-11 00:00:00.000           1
3.00      2014-02-03 00:00:00.000           1
2.00      2014-02-05 00:00:00.000           1

我想将我的输出显示为

January 3.00
February 5.00
!!!!!!

December 12.00

那么,我如何计算该金额以及如何将所有天数归为一个月

我使用的是 codeigniter 3.0,我的数据库是 sql server 2012

public function overviews(){
 //   $merchantid=$this->session->userdata('user_password');
   $baji=$this->livemerchant_model->overviews();
    $name=$this->session->userdata('name');
           $data=array('baji'=>$baji,'name'=>$name);

   if($this->session->has_userdata('user_email','name')){
    $this->load->view('overviews.php', $data);


     }
   }

sfnerd 这是我的控制器,是否可以显示像

这样的数据
select year 
january  12.00
february 24.00


Total    36.00

我只想显示 selected 年的金额计数

<label class="tp-label">Select a year</label>
                                                              <select>
                                                               <?php
foreach($monthlyTotals as $row)
{
?>
<option value = "<?php echo $row->TheYear?>"><?php echo $row->TheYear;?>    </option>
<?php
}
?>

所以当我执行此循环时,我会根据 2014 年这样的月份重复数年 12 次,我需要像 select 2015 年这样的输出,并且当我单击该按钮时需要加载按钮显示 12 个月

public function overviews()
{
$this->load->model('livemerchant_model');
$name=$this->session->userdata('name');
$data['monthlyTotals'] = $this->livemerchant_model-  >overviews($theyear='');
$this->load->view('overviews', $data);
} 

并使用了这个控制器

帮帮我

可能有几种方法可以做到这一点,但这里有两种方法(一种使用 Active Record,一种直接 SQL)。我使用 CI 3.0.6 和 SQL Server 2012 进行了测试,并创建了一个类似的 table.

模型函数(假设模型称为客户)

直SQL版本:

function get_monthly_totals($theYear = '', $theMonth = '')
{
    $sql = "SELECT DATEPART(Year, Date) [TheYear], DATEPART(Month, Date) [TheMonth], DATENAME(Month, Date) [TheMonthName], SUM(Amount) [TotalAmount]
        FROM Customers
        WHERE Date IS NOT NULL ";

    if ($theYear != '') {
        $sql = $sql . " AND DATEPART(Year, Date) = '$theYear'";
    }

    if ($theMonth != '') {
        $sql = $sql . " AND DATEPART(Month, Date) = '$theMonth'";
    }

    $sql = $sql . " GROUP BY DATEPART(Year, Date), DATEPART(Month, Date), DATENAME(Month, Date) ORDER BY [TheYear], [TheMonth], [TheMonthName]";

    $query = $this->db->query($sql);

    return $query->result();
}

Active Record 版本:

function get_monthly_totals($theYear = '', $theMonth = '')
{

    $this->db->select("DATEPART(Year, Date) [TheYear], DATEPART(Month, Date) [TheMonth], DATENAME(Month, Date) [TheMonthName], SUM(Amount) [TotalAmount]", false);
    $this->db->from('Customers');

    if ($theYear != '') {
        $this->db->where("DATEPART(Year, Date) = '" . $theYear . "'", NULL, FALSE);
    }

    if ($theMonth != '') {
        $this->db->where("DATEPART(Month, Date) = '" . $theMonth . "'", NULL, FALSE);
    }

    $this->db->group_by("DATEPART(Year, Date), DATEPART(Month, Date), DATENAME(Month, Date)");
    $this->db->order_by("1", "asc");
    $this->db->order_by("2", "asc");
    $this->db->order_by("3", "asc");

    $query = $this->db->get();

    return $query->result();
}

控制器功能

public function index()
{
    $this->load->model('Customers_model');

    $data['monthlyTotals'] = $this->Customers_model->get_monthly_totals('2014');

    $this->load->view('customers', $data);
}

查看输出 (customers.php)

<?php 
    foreach ($monthlyTotals as $row)
    {
        echo $row->TheYear . " - " . $row->TheMonthName . ": " . $row->TotalAmount . " <br />" . PHP_EOL;
    }
?>

在我的例子中,我做了以下代码

public function earning_chart_year($DriverId,$year){
    $this->db->select_sum('driverCommision');
    $this->db->select('MONTHNAME(displayTime) as createDate');        
    $this->db->group_by('monthname(displayTime)');
    $this->db->where('driverId',$DriverId);
    $this->db->where('year(displayTime)',$year);
    $result=$this->db->get('BookingMaster')->result_array();
    return $result;
}

输出:

Array ( [0] => Array ( [totalBooking] => 52 [createDate] => November )

)