Codeigniter Select_Sum returns an "array" is not a numerical value?

I needed to collect all the rows for the result. Using select_sum as follows

Here is a model

    function Dues_Paid_Tot($date)
{
    $query = $this->db->select_sum('Dues_Paid', 'Dues_Paid_Tot');
    $query = $this->db->get('Membership');
    return $query->result();
}

Here is the controller

    function Fiscal2()
{
$date = $this->input->post('Select_Date');
    if($query = $this->report_model->fiscal_list($date))
    {
        $data['records'] = $query;
    }
$data['date'] = $this->input->post('Select_Date');
$data['Dues_Paid_Tot'] = $this->report_model->Dues_Paid_Tot($date);
$data['main_content'] = 'report_fiscal_view';
$this->load->view('includes/template', $data);
}

And this is the appropriate code to represent

<?php echo $Dues_Paid_Tot; ?>

The problem is that instead of summing all the entries in the Dues_Paid column, I get an "Array" in my opinion.

Where am I going wrong?

Thank!

0
source share
3 answers

This is still a request, so you still have to process the result, try changing your model to this ...

function Dues_Paid_Tot($date)
{
    $query = $this->db->select_sum('Dues_Paid', 'Dues_Paid_Tot');
    $query = $this->db->get('Membership');
    $result = $query->result();

    return $result[0]->Dues_Paid_Tot;
}
+2
source

You are not doing anything wrong, this is how CodeIgniter works. From the manual toresult() :

.

, , , .

var_dum($data['Dues_Paid_Tot']);, , , , - :

$data['Dues_Paid_Tot'][0]->some_name
+1
function Dues_Paid_Tot($date)
{
    $query = $this->db->select_sum('Dues_Paid', 'Dues_Paid_Tot');
    $query = $this->db->get('Membership');
    return $query->result();
}

return $query->result();

This function returns the query result as an array of objects, or an empty array on failure.

For more information see http://codeigniter.com/user_guide/database/results.html

+1
source

All Articles