这段 PHP 代码有什么问题?

发布于 2024-11-06 11:57:24 字数 1611 浏览 0 评论 0原文

我有两个数据库表,一张用于津贴,一张用于扣除。我想计算净工资。

我正在使用 CodeIgniter。这是我当前的代码:

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

在控制器中,

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

我的 net_salary() 函数给出的结果为 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

念三年u 2024-11-13 11:57:24

具有复数名称的模型只会返回一个对象

所以你最终得到的是......

Array
(
    [0] => allowance_object
)

虽然

Array
(
    [0] => deduction_object
)

我们确实需要你的数据库的模式尝试这个(并对扣除进行相同的编辑)......

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

然后在你的 net_salary 函数中

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

    return array_sum($allownce) - array_sum($deduction);
}

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);
}
无敌元气妹 2024-11-13 11:57:24

尝试这样的操作:

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

其中 value_column_name 是所需值所在的表列的名称(filedname)。

调用控制器:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文