Search code examples
phpmysqlcodeignitersessionactiverecord

Get the sum of a column (Codeigniter)


newbie here. How can I get the total sum of the "amount" column? I tried working on a function but it's not dynamic. It only reads the column of my main account. But when i logged on another account, The total amount is still the same. My target is, how can i make it dynamic based on userID? Thank you in advance

enter image description here

Views:

<div class="small-box bg-success">
  <div class="inner">
    <h3><?php echo $tcash;?><sup style="font-size: 20px"> Php</sup></h3>

    <p><b>Total Cash-In</b></p>
  </div>
  <div class="icon">
    <i class="fas fa-coins"></i>
  </div>

Controller:

public function cashout()
{
    $data['activeNav'] = "";
    $data['subnav'] = "ewallets";
    $this->header($data);
    $this->nav();
    

    $data['tcash']=$this->load->ewallets->gettotalcashin();
    
    $this->load->view('cashout', $data);
    
    $this->footer();
}

Model:

function gettotalcashin(){
    
    $this->db->select_sum('amount');
    $result = $this->db->get('cash_out')->row();
    return $result->amount;
}

Solution

  • Solved already.

    Model:

    function gettotalcashin(){
        $reqamount= $this->session->userdata('username');
            
        $this->db->select_sum('amount');
        $this->db->where('userID',$reqamount);
        $result = $this->db->get('cash_in')->row();
            
        return $result->amount;
    }