0

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>
0

2 Answers 2

1

Try like

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

Since it will be returned as object.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried and receive: Undefined index: urls. Do you have another idea? Thank you so far.
Can you print the $data['sum'] in your controller and tell me
array(1) { [0]=> object(stdClass)#22 (1) { ["urls"]=> string(5) "18294" } }
0
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;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.