计数数据每月使用codeigniter

时间:2016-04-12 17:12:28

标签: codeigniter sqlsrv

我正在使用Codeigniter 3.0和sql Server

这是我的表

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

我只想选择年份的金额显示计数

<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次的几个月,我需要多年,我需要输出像选择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);
} 

并使用此控制器

帮帮我

2 个答案:

答案 0 :(得分:0)

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

模型功能(假设模型称为客户)

直接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;
    }
?>

答案 1 :(得分:0)

就我而言,我遵循以下代码

f(x-3) + (x+5)

输出:

  

数组   (       [0] =>数组           (               [totalBooking] => 52               [createDate] =>十一月           )