CodeIgniter PHP组合Database中的所有行以显示一个值

时间:2016-12-22 15:51:15

标签: php mysql codeigniter group-by sum

我正在寻求帮助,我希望显示一个数值,这是我的表的组合,看起来像这样

FinPlanIncomeID (PK)
FinancialPlanID (FK)
Weekly (int)
Fortnightly (int)
Monthly (int)

我希望每周每周和每月的所有行都加起来制作一个主值,所以到目前为止我所做的就是这个。

Model.php

    public function getIncome($data) {            

    $condition = "Debt.[DebtID] = '".$data."' AND ";    
    $condition .= "fp.[FinancialPlanID] = fpi.[FinancialPlanID] AND ";
    $condition .= "Debt.[DebtID] = fp.[DebtID] AND ";
    $condition .= "fp.[Active] = 1 AND ";
    $condition .= "Debt.[clientid] IN (SELECT [client_id] FROM [ClientPortal].[dbo].[ci_user_access] WHERE [user_id] = '".$this->session->userdata['logged_in']['id']."')";


    $this->db->select ('fp.[DebtID], fp.[Active], fpi.[FinancialPlanID], fpi.[weekly], fpi.[fortnightly], fpi.[monthly], fpi.[weekly]*4 AS totalweekly, fpi.[monthly] AS totalmonthly, fpi.[fortnightly]*2 AS totalfortnightly');
    $this->db->from('[Focus].[dbo].[FinPlanIncome] AS fpi');
    $this->db->join('[Focus].[dbo].[FinancialPlan] AS fp', 'fp.[FinancialPlanID] = fpi.[FinancialPlanID]');
    $this->db->join('[Focus].[dbo].[Debt] AS Debt', 'fp.[DebtID] = Debt.[DebtID]');
    $this->db->where($condition);
    $query = $this->db->get();

    return $query->result_array();
            }

Controller.php这样

   public function overview()
    {


    if($this->_core())
    {
        $income = $this->accounts_database->getincome($this->session->userdata('debtid'));

        $data = array(
            'income1'       => $income[0]['totalmonthly'],
            'income2'       => $income[0]['totalfortnightly'],
            'income3'       => $income[0]['totalweekly']
            );

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

    }
}

View.php

<td>Income: <?php  $total = $income1 + $income2 + $income3; print($total); ?> </td>

我试图完成大部分代码,只是为了向您展示重要的部分。所以简而言之,我试图将每周,每两周和每月的所有行合并为一个主表作为$ total在视图中,以便它们显示值,我在那里工作,但它只添加我的第一行和不做其余的事。

任何帮助都会很棒。

编辑:使用SQL

SELECT 
      ISNULL((SUM (ISNULL(fpi.weekly, 0)) * 4) + SUM (ISNULL(fpi.monthly, 0)) + (SUM (ISNULL(fpi.fortnightly, 0)) * 2), 0) 
FROM FinancialPlan AS fp
INNER JOIN dbo.FinPlanIncome AS fpi 
    ON fp.FinancialPlanID = fpi.FinancialPlanID
WHERE DebtID = 1
AND Active = 1`

0 个答案:

没有答案