如何使用Codeigniter在mysql tabe数据中进行正确的计算

时间:2016-11-24 13:37:01

标签: php mysql codeigniter

我有一个名为transactions的表,结构如下

id      dr_balance    cr_balance  initial_balance
1        0.00          10000.00       10000.00
2        0.00           5000.00
3       2000.00            0.00

我想像这样展示

id      dr_balance    cr_balance  initial_balance
1        0.00          10000.00       10000.00
2        0.00           5000.00       15000.00
3       2000.00            0.00      13000.00

我的模态

    function make_calculation()
{
    $query = $this->db->select_sum('cr', 'dr');
    $query = $this->db->get('accounts_transactions');
    $result = $query->result();

    return $result[0]->make_calculation;
}

我的控制器

public function new_deposit()
{
    $this->load->model('Accounts_transaction_model');
    $data['calculation'] = $this->Accounts_transaction_model->make_calculation();
    $this->load->view('template/header');
    $this->load->view('transaction/new_deposit', $data);
    $this->load->view('template/footer');
}

我的观点

               <?php
                   foreach ($calculation as $key ):
               ?>

             <?php  
                 $cr = $key['cr'];
                 $dr = $key['cr'];
                 $initial = $cr - $dr;
             ?>
             <input type="text" value="<?php echo $initial; ?>" name="initial" />
             <?php  endforeach; ?>

错误如下: -

A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$make_calculation

Filename: models/Accounts_transaction_model.php

Line Number: 51

Backtrace:

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: transaction/new_deposit.php

Line Number: 16

Backtrace:

我希望将cr_balance的总和减去dr_balance,然后将结果插入initial_balance。将为每个新行更新此计算。我需要一些帮助。

0 个答案:

没有答案