PHP/Codeigniter Logical Operator

2019-08-29 05:18发布

问题:

$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.