0

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?

3
  • 3
    Aren't you setting all your deductions to '' in the foreach() loops, essentially destroying the data you retrieved from the database? As well, array_sum() expects an ARRAY. You're passing in an object, so array_sum sees an empty array and returns 0. Commented May 9, 2011 at 18:27
  • consider mergin those two methods - they do exactly the smae - $table_name could be the parameter Commented May 9, 2011 at 18:31
  • @Marc B - He's only setting setting the allowances/deductions to '' if his query does not return exactly one result. I think the issue is that array_sum() doesn't work with an object(like you said). Commented May 9, 2011 at 18:37

2 Answers 2

1

Your models with plural names are only going to return a single object.

so what you are ending up with is...

Array
(
    [0] => allowance_object
)

and

Array
(
    [0] => deduction_object
)

While we really need the schema of your database try this (and make same edits for deductions)...

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_array();  //<--- return an Array
    }
    else
    {
        // make an array instead of object
        $salary_obj = array();

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

        foreach ($fields as $field)
        {
            $salary_array[$field] = 0;   //<---- add array keys and set to integer 0 instead of empty string.
        }

        return $salary_array;
    }
}

then in your net_salary function

function net_salary($eid)
{
    $allownce = $this->Salary->get_allowances($eid);
    $deduction = $this->Salary->get_deductions($eid);

    return array_sum($allownce) - array_sum($deduction);
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you i m thinking now if i use array_sum this way them eid and id will be sum what is the best solution to do for salary if let me inform thank u
0

Try something like this:

function get_values($eid, $table_name)
{
    $this->db->where('eid',$eid);
    $query = $this->db->get($table_name);

    $salary_obj = $query->result();

    $values = array();
    foreach($salary_obj as $row){
        $values[] = $row->value_column_name;
    }

    return $values;
}

where value_column_name is the name of the table column (filedname) where the desired value stands.

call in controller:

function net_salary($eid)
{
    $allownces = $this->Salary->get_values($eid, 'allowances');
    $deductions = $this->Salary->get_values($eid, 'deductions');

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

1 Comment

thank you i m thinking now if i use array_sum this way them eid and id will be sum what is the best solution to do for salary if let me inform thank u

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.