Masud
Masud

Reputation: 1

What is wrong with this PHP code?

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?

Upvotes: 0

Views: 292

Answers (2)

jondavidjohn
jondavidjohn

Reputation: 62412

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);
}

Upvotes: 1

helle
helle

Reputation: 11660

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);
}

Upvotes: 0

Related Questions