将总和保存在我的 table (codeigniter)

Save the total sum in my table (codeigniter)

这里是 codeigniter 的新手。如何将每个用户的总金额保存在 table 中?我唯一做的就是总结它并在我的视图中显示它。我的目标是节省我 "agent_commission" table" 的总佣金。我提供了我的目标的屏幕截图以便更好地可视化。提前感谢您. 任何帮助将不胜感激。

观看次数:

<?php $formattedNum = number_format($commCurrentBalance, 2);
                            echo $formattedNum;
                            ?

控制器:

public function commi()
    {
        $data['activeNav'] = "";
        $data['subnav'] = "commissions";
        $this->header($data);
        $this->nav();
        $data['currentPoints']=$this->session->userdata('currentPoints');
        $data['result'] = $this->commissions->get1();
        $data['commCurrentBalance']=$this->load->commissions->gettotalcommi(); //getting sum of amount
        $this->load->view('comm_view', $data);
        
        $this->footer();
    }

型号:

function gettotalcommi(){
        $reqcommi= $this->session->userdata('uid');
        
        $this->db->select_sum('amount');
        $this->db->where('commTo',$reqcommi);
        $result = $this->db->get('agent_commission_history')->row();
        
        return $result->amount;
    }
public function commi(){
 $data['commCurrentBalance']=$this->load->commissions->gettotalcommi(); //getting sum of amount
 
$dataArray = array(
        'commCurrentBalance'=>$data['commCurrentBalance']
        );
 
 $this->db->insert('agent_commission',$dataArray);
 
 $this->load->view('comm_view', $data);
        
        $this->footer();
    }

我不确定它是否有效,但请尝试

$db->select_sum('amount');
$db->where('commTo',$param);
$aq = $db->get_compiled_select('agent_commission_history');

$db->set('commCurrentBalance',"($aq)",false);
$db->where('commTo',$param2);
$db->update('agent_commission');

这是我之前发布的查询的 Codeigniter 等价物:

UPDATE agent_commission 
SET commCurrentBalance = ( SELECT SUM(amount) FROM agent_commission_history WHERE commTo = ?) 
WHERE commTo = ?