Hobby99
Hobby99

Reputation: 703

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>

Upvotes: 0

Views: 6760

Answers (2)

Pushpendra Kumar
Pushpendra Kumar

Reputation: 1674

public function get_sum($id)
{
    $this->db->select('SUM(urls) as total');
    $this->db->where('user_id', $id);
    $q=$this->db->get('user_earnings');
    $row=$q->row();
    echo $row->url;
}

Upvotes: 0

GautamD31
GautamD31

Reputation: 28763

Try like

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

Since it will be returned as object.

Upvotes: 1

Related Questions