Search code examples
phpmysqlcodeigniteractiverecordsum

Get sum of mysql fields and passing sum to view in codeigniter


I am not able to display the sum of mysql fields in the view. The sum-field is named urls. Instead of displaying the sum of all urls for the specific user I receive a string named 'array'. I believe it is a passing problem rather than a query one but I don't get it. Thank you for your help.

Model (users_model):

public function get_sum($id){
    $this->db->select_sum('urls')
            ->where('user_id', $id);
    $query = $this->db->get('user_earnings');
    return $query->result();
}

Controller (users):

public function userarea() {
    $id = $this->session->userdata('id');
    $data['sum'] = $this->users_model->get_sum($id);
    $data['main_content'] = 'userarea_view';
    $this->load->view('layout', $data);
}

View (userarea_view):

<li>Total URLs Collected: <br><strong><?php echo $sum; ?></strong></li>

Solution

  • Try like

    <?php echo $sum[0]->urls; ?>
    

    Since it will be returned as object.