PHP/Codeigniter Logical Operator

試著忘記壹切 提交于 2020-01-07 03:22:09

问题


$total_cost_for_A = $this->admin_model->total_cost_for_A($branch, $department, $date);
$total_cost_for_B = $this->admin_model->total_cost_for_B($branch, $department, $date);

The variables $total_cost_for_A and $total_cost_for_B hold the returned value from a mysql summation queries in the model. The queries return false if no record was found. Now, I'm trying to perform this operation.

if(($total_cost_for_A === FALSE) && ($total_cost_for_B === FALSE))
{
  $data['no_record'] = 'No Record Found!';
  $this->load->view('admin/bookings', $data);
}
else
{
    $this->load->view('admin/summary', $data);
}

This test always fail and execute the else statement instead, which is not what. Any assistance will be appreciated. Thanks

Here are the functions

    function total_cost_for_A($branch, $department, $date)
    {
        if($branch == 'Head Office' && $department == 'Summary')
        {
            $select_amount_paid = 'SELECT SUM(total_amount) total_amount FROM graphics_booking AS bk
            WHERE bk.date = "'.$date.'"';
        }

        $result = $this->db->query($select_amount_paid);

        if($result->num_rows() > 0)
        {
            return $result;
        }
        else
        {
            return FALSE;
        }
    }


    function total_cost_for_B($branch, $department, $date)
    {
        if($branch == 'Head Office' && $department == 'Summary')
        {
            $total_LF_amount = 'SELECT SUM(total_amount) total_amount FROM large_format_print
            WHERE date = "'.$date.'"';
        }

        $result = $this->db->query($total_LF_amount);

        if($result->num_rows() > 0)
        {
            return $result;
        }
        else
        {
            return FALSE;
        }
    }

回答1:


Empty objects are not false.

As for your comment, please make sure that total_cost_for_A and B are actually returning something. And make sure that they are returning false on failure. That is the only way your conditional will evaluate to true.

You are using strict equality comparison so they will never equal false unless you return false!

if(($total_cost_for_A == FALSE) && ($total_cost_for_B == FALSE))

The above should work because empty arrays are false.




回答2:


It seems your comparisons need to use $total_cost_for_A and $total_cost_for_B

you are missing "for" from your variable names.



来源:https://stackoverflow.com/questions/12323539/php-codeigniter-logical-operator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!