PHP嵌套ifs和许多条件不起作用

时间:2014-09-11 07:12:33

标签: php codeigniter

我的代码中有很多嵌套的ifs和条件,但它没有提供所需的输出。编写以下代码的最佳方法是什么:

$driver_code = $this->input->post('filter_driver_code');

        $unit_code = $this->input->post('filter_unit_code');

        $fuel_type = $this->input->post('filter_fuel');

        $date_to = $this->input->post('date_to');

        $date_from = $this->input->post('date_from');





        if (isset($date_from) and isset($date_to) and empty($unit_code) and empty($driver_code) and empty($fuel_type)) {

            $sql = "SELECT * FROM fuel_usage where (date between '$date_from' and '$date_to') ";
            $result = $this->db->query($sql);
        } elseif (isset($driver_code) and isset($unit_code) and isset($fuel_type) and isset($date_from) and isset($date_to)) {

            $sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type' and (date between '$date_from' and '$date_to')";
            $result = $this->db->query($sql);
        }  elseif (empty ($date_from) and empty ($date_to) and isset ($unit_code) and isset ($driver_code) and isset ($fuel_type)) {

            $sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type'";
            $result = $this->db->query($sql);
        }  else {
            $sql="SELECT * FROM FUEL_USAGE";
            $result = $this->db->query($sql);
        }

它没有给出正确的输出。

2 个答案:

答案 0 :(得分:1)

尝试使用empty()进行所有检查可能是isset()为您提供问题原因对于所有变量的post isset将返回true,即使值为空白

       if (!empty($driver_code) && !empty($unit_code) && !empty($fuel_type) && !empty($date_from) && !empty($date_to)) {
            $sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type' and (date between '$date_from' and '$date_to')";
            $result = $this->db->query($sql);
        }elseif (!empty($date_from) && !empty($date_to) && empty($unit_code) && empty($driver_code) && empty($fuel_type)) {
            $sql = "SELECT * FROM fuel_usage where (date between '$date_from' and '$date_to') ";
            $result = $this->db->query($sql);
        }  elseif (empty($date_from) && empty($date_to) && !empty($unit_code) && !empty($driver_code) && !empty($fuel_type)) {
            $sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type'";
            $result = $this->db->query($sql);
        }  else {
            $sql="SELECT * FROM FUEL_USAGE";
            $result = $this->db->query($sql);
        }

或以简短的方式尝试

$sql = "SELECT * FROM fuel_usage where 1 ";
if (!empty($driver_code)) {
  $sql .= " AND driver_code='$driver_code' ";
} 
if (!empty($unit_code)) {
  $sql .= " AND unit_code='$unit_code' ";
} 
if (!empty($fuel_type)) {
  $sql .= " AND fuel_type='$fuel_type' ";
} 
if (!empty($date_from) && !empty($date_to)) {
  $sql .= " AND date between '$date_from' and '$date_to' ";
}
$result = $this->db->query($sql); 

答案 1 :(得分:-1)

    if (!empty($driver_code) && !empty($unit_code) && !empty($fuel_type) && !empty($date_from) && !empty($date_to)) {

        $sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type' and (date between '$date_from' and '$date_to')";
        $result = $this->db->query($sql);
    }
    elseif(!empty ($unit_code) && !empty ($driver_code) && !empty ($fuel_type)) {

        $sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type'";
        $result = $this->db->query($sql);
    }
    elseif (!empty($date_from) and !empty($date_to)) {

        $sql = "SELECT * FROM fuel_usage where (date between '$date_from' and '$date_to') ";
        $result = $this->db->query($sql);
    }  else {
        $sql="SELECT * FROM FUEL_USAGE";
        $result = $this->db->query($sql);
    }