Subtract numeric related data between two tables using CodeIgniter's query builder
11:40 09 May 2011

I have two database tables, one for allowances and one for deductions. I want to calculate net salaries.

I'm using CodeIgniter. Here's my current code:

function get_allowances($eid)
{
    $this->db->from('allowances');
    $this->db->where('eid', $eid);
    $query = $this->db->get();

    if ($query->num_rows() == 1) {
        return $query->row();
    } else {
        //Get empty base parent object, as $item_id is NOT an item
        $salary_obj = new stdClass();

        //Get all the fields from items table
        $fields = $this->db->list_fields('allowances');

        foreach ($fields as $field) {
            $salary_obj->$field = '';
        }

        return $salary_obj;
    }
}

function get_deductions($eid)
{
    $this->db->from('deductions');
    $this->db->where('eid', $eid);
    $query = $this->db->get();

    if ($query->num_rows() == 1) {
        return $query->row();
    } else {
        //Get empty base parent object, as $item_id is NOT an item
        $salary_obj = new stdClass();

        //Get all the fields from items table
        $fields = $this->db->list_fields('deductions');

        foreach ($fields as $field) {
            $salary_obj->$field = '';
        }

        return $salary_obj;
    }
}

and in controller,

function net_salary($eid)
{
    $allownces[] = $this->Salary->get_allowances($eid);
    $deductions[] = $this->Salary->get_deductions($eid);

    return $net_salary = array_sum($allownces) - array_sum($deductions);
}

My net_salary() function gives me a result of 0. What am I doing wrong, and how can I fix it?

php codeigniter query-builder subtraction